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 } }
https://viagrameilleurprix.shop/# Viagra sans ordonnance livraison 24h
pharmacie en ligne france livraison internationale
A superb-high quality ceiling fixture or recessed or observe lighting will prove invaluable.
https://pharmaciemeilleurprix.shop/# trouver un mГ©dicament en pharmacie
Pharmacie en ligne livraison Europe
Viagra vente libre pays Viagra sans ordonnance 24h Viagra prix pharmacie paris
https://pharmaciemeilleurprix.com/# pharmacie en ligne
pharmacie en ligne sans ordonnance
Gerard Hamilton McConnell, Assistant Under-Secretary of State, House Workplace.
pharmacie en ligne: pharmacie en ligne – pharmacie en ligne avec ordonnance
Do you mind if I quote a few of your posts as long as I provide credit and sources back to your blog? My blog is in the exact same area of interest as yours and my users would genuinely benefit from some of the information you present here. Please let me know if this okay with you. Many thanks!
I have seen loads of useful items on your internet site about computer systems. However, I have got the judgment that lap tops are still less than powerful sufficiently to be a option if you frequently do projects that require many power, just like video croping and editing. But for world-wide-web surfing, word processing, and most other common computer work they are all right, provided you can’t mind small screen size. Thank you sharing your opinions.
Great post. I was checking continuously this blog and I’m impressed! Extremely helpful info specifically the last part 🙂 I care for such information a lot. I was seeking this certain info for a long time. Thank you and best of luck.
pharmacie en ligne livraison europe: Tadalafil sans ordonnance en ligne – pharmacie en ligne pas cher
Thanks for the diverse tips provided on this web site. I have realized that many insurance companies offer customers generous special discounts if they elect to insure a few cars with them. A significant number of households own several vehicles these days, specifically those with older teenage children still located at home, and also the savings in policies can soon mount up. So it will pay to look for a bargain.
I was suggested this web site via my cousin. I’m now not sure whether this submit is written through him as no one else recognise such detailed about my difficulty. You are amazing! Thank you!
Thanks for your post. Another item is that to be a photographer will involve not only issues in recording award-winning photographs but hardships in getting the best photographic camera suited to your needs and most especially problems in maintaining the grade of your camera. This is certainly very true and noticeable for those photography addicts that are into capturing the particular nature’s fascinating scenes — the mountains, the forests, the particular wild or seas. Going to these daring places definitely requires a photographic camera that can live up to the wild’s unpleasant conditions.
Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch because I found it for him smile Therefore let me rephrase that: Thank you for lunch!
pharmacie en ligne fiable https://pharmaciemeilleurprix.shop/# Pharmacie sans ordonnance
Your home is valueble for me. Thanks!?
I do accept as true with all of the ideas you have presented for your post. They’re really convincing and will certainly work. Still, the posts are too short for beginners. May just you please lengthen them a bit from next time? Thanks for the post.
pharmacie en ligne avec ordonnance http://viagrameilleurprix.com/# п»їViagra sans ordonnance 24h
You actually make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I?ll try to get the hang of it!
hey there and thank you on your info ? I have definitely picked up anything new from proper here. I did however expertise some technical points the usage of this web site, as I skilled to reload the site many occasions previous to I could get it to load correctly. I have been thinking about if your web host is OK? No longer that I’m complaining, but sluggish loading instances occasions will very frequently have an effect on your placement in google and could injury your high quality rating if advertising and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I am adding this RSS to my e-mail and could glance out for a lot more of your respective interesting content. Ensure that you replace this again soon..
Good write-up, I am regular visitor of one?s web site, maintain up the nice operate, and It’s going to be a regular visitor for a long time.
Hi there, I found your web site via Google while searching for a related topic, your web site came up, it looks good. I’ve bookmarked it in my google bookmarks.
Howdy! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept chatting about this. I will forward this article to him. Fairly certain he will have a good read. Thank you for sharing!
I believe one of your adverts triggered my web browser to resize, you might want to put that on your blacklist.
One thing I’d prefer to say is that before obtaining more computer memory, look at the machine into which it could be installed. Should the machine can be running Windows XP, for instance, the actual memory threshold is 3.25GB. Installing more than this would basically constitute any waste. Make sure one’s motherboard can handle your upgrade amount, as well. Interesting blog post.
Ninety Winternitz, History, II: 343-44, n.
I have been exploring for a little bit for any high quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this information So i am happy to convey that I’ve an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make certain to do not forget this web site and give it a glance regularly.
The core of your writing while appearing reasonable in the beginning, did not work properly with me personally after some time. Somewhere throughout the paragraphs you managed to make me a believer but only for a very short while. I nevertheless have got a problem with your jumps in assumptions and you would do well to help fill in all those breaks. In the event that you actually can accomplish that, I could surely be fascinated.
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is fantastic blog. A great read. I will definitely be back.