Java 各種時間上的操作範例
分享一些在專案中用到JAVA與日期相關的操作,包括:
- 取得目前的年、月、日
- 判斷兩個日期的大小
- 計算兩個日期的差距
- 取得昨天的日期
- 取得上個月的開始與結束日
package CDIT.stanley; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.Calendar; public class dateOperation { public static int differentDays(Date date1,Date date2){ Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); int day1= cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); if(year1 != year2){ int timeDistance = 0 ; for(int i = year1 ; i < year2 ; i ++){ if(i%4==0 && i%100!=0 || i%400==0){ timeDistance += 366; } else{ timeDistance += 365; } } return timeDistance + (day2-day1) ; } else{ return day2-day1; } } public static Date getFirstMonthDay(Calendar calendar) { calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE)); return calendar.getTime(); } public static Date getLastMonthDay(Calendar calendar) { calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE)); return calendar.getTime(); } public static void main(String[] args) throws ParseException { //取得目前的年、月、日 Calendar calendar = Calendar.getInstance(); System.out.println("今天是" + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日"); System.out.println("=================================================="); //輸出:今天是2017年8月24日 //判斷兩個日期的大小 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.TAIWAN); Date date1 = sdf.parse("2017-08-23"); Date date2 = sdf.parse("2016-09-22"); System.out.println("Date1 < Date2 : " + date1.before(date2)); System.out.println("Date1 > Date2 : " + date1.after(date2)); System.out.println("=================================================="); //輸出:Date1 < Date2 : false、Date1 > Date2 : true //計算兩個日期的差距 System.out.println("Date1 & Date2 差距 : " + differentDays(date1 , date2) + "天"); System.out.println("=================================================="); //輸出:Date1 & Date2 差距 : 31天 //取得昨天的日期 calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); String yestedayDate = sdf.format(calendar.getTime()); System.out.println("昨天是" + yestedayDate); System.out.println("=================================================="); //輸出:昨天是2017-08-23 //取得上個月的開始與結束日 calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,-1); String monthDayFirst = sdf.format(getFirstMonthDay(calendar)); String monthDayLast = sdf.format(getLastMonthDay(calendar)); System.out.println("上個月的第一天是" + monthDayFirst); System.out.println("上個月的最後一天是" + monthDayLast); System.out.println("=================================================="); //輸出:上個月的第一天是2017-07-01、上個月的最後一天是2017-07-31 } }
cheap clomid now: ClmHealthPharm – can i get generic clomid without prescription
Audio began playing any time I opened up this website, so irritating!
Just about all of what you mention happens to be supprisingly precise and it makes me wonder the reason why I had not looked at this in this light before. This particular article truly did turn the light on for me personally as far as this topic goes. Nevertheless there is actually one particular position I am not too comfortable with so whilst I make an effort to reconcile that with the core idea of your position, allow me observe what all the rest of the readers have to point out.Very well done.
https://doxhealthpharm.shop/# doxycycline canada
zithromax z-pak zithromax tablets buy azithromycin zithromax
http://doxhealthpharm.com/# rx doxycycline 100mg
price doxycycline: Dox Health Pharm – doxycycline online with no prescription
how can i get clomid tablets how to get cheap clomid tablets get cheap clomid online
I have seen that car insurance companies know the vehicles which are liable to accidents and also other risks. They also know what types of cars are given to higher risk and also the higher risk they’ve the higher your premium rate. Understanding the simple basics connected with car insurance will assist you to choose the right kind of insurance policy that should take care of your requirements in case you happen to be involved in an accident. Thank you sharing the particular ideas on the blog.
buy generic clomid: ClmHealthPharm – generic clomid without rx
You can too make a profile and start taking jobs on a platform like Process Rabbit or providing rides on Uber or Lyft, if you happen to personal a car.
amoxicillin canada price: amoxicillin online no prescription – 875 mg amoxicillin cost
However there are particular actions that traders and investors can take in order to have that edge and get ahead in the financial world.
amoxicillin generic: where to buy amoxicillin – amoxicillin 500 mg
can i purchase generic clomid: ClmHealthPharm – where to buy cheap clomid without dr prescription
where to buy clomid online: ClmHealthPharm – where to buy generic clomid without a prescription
where buy generic clomid no prescription: ClmHealthPharm – where buy cheap clomid without rx
amoxicillin no prescription Amo Health Pharm amoxicillin 500mg capsules price
http://clmhealthpharm.com/# can i buy clomid prices
What i don’t realize is actually how you are not actually much more well-liked than you may be now. You’re very intelligent. You realize thus considerably relating to this subject, produced me personally consider it from a lot of varied angles. Its like men and women aren’t fascinated unless it?s one thing to accomplish with Lady gaga! Your own stuffs outstanding. Always maintain it up!
https://amohealthpharm.shop/# how to buy amoxycillin
amoxicillin 250 mg capsule: AmoHealthPharm – amoxicillin 875 mg tablet
doxycycline hydrochloride: Dox Health Pharm – doxycycline tablets in india
cost of clomid pills: where buy cheap clomid pill – order cheap clomid pills
clomid sale: can i get generic clomid pills – how can i get clomid for sale
amoxicillin 1000 mg capsule: Amo Health Pharm – amoxicillin price canada
My brother recommended I might like this web site. He was entirely right. This post truly made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!
MachFi: Revolutionizing DeFi with Sonic Chain.
MachFi is leading the way in decentralized finance (DeFi), offering a next-gen borrow-lending platform on the Sonic Chain. Our platform supports customizable trading strategies, giving users more control over their assets in a secure, decentralized ecosystem. visit to https://machfi.net/
Why Choose MachFi?
– Decentralized: Powered by the Sonic Chain for transparency and security.
– Flexible Borrow-Lending: Tailored to your financial goals with custom trading strategies.
– Innovative Technology: Harness the power of the latest blockchain technology to maximize yields.
Start your journey with MachFi today and experience the future of DeFi!
Like every other investment that you just make, it would be wise to look into all the details of investing in commodity mutual funds.
buy doxycycline no prescription: DoxHealthPharm – doxycycline canada price