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 } }
reputable canadian online pharmacy: canadian neighbor pharmacy – USACanadaPharm
canadian pharmacy prescription drugs canada buy online usa canada pharm
Almost all of what you point out happens to be astonishingly accurate and that makes me ponder why I hadn’t looked at this with this light previously. This particular piece really did switch the light on for me personally as far as this topic goes. But at this time there is actually just one point I am not too cozy with so whilst I make an effort to reconcile that with the core idea of the point, permit me observe just what all the rest of the subscribers have to say.Very well done.
https://usacanadapharm.shop/# USACanadaPharm
legit canadian online pharmacy: my canadian pharmacy – legit canadian online pharmacy
useful content https://binslist.com
Greetings! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.
usa canada pharm: usa canada pharm – online canadian pharmacy
I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are too short for novices. Could you please extend them a little from next time? Thanks for the post.
Hey! I simply wish to give an enormous thumbs up for the nice info you’ve gotten right here on this post. I shall be coming again to your blog for more soon.
usa canada pharm: usa canada pharm – canadian pharmacy meds
https://usacanadapharm.com/# USACanadaPharm
I love what you guys are up too. This kind of clever work and coverage! Keep up the terrific works guys I’ve incorporated you guys to my own blogroll.
canadian pharmacy 365 canadian pharmacy checker canadian pharmacy in canada
WONDERFUL Post.thanks for share..extra wait .. ?
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 info So i?m happy to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this website and give it a glance regularly.
This article is a refreshing change! The author’s distinctive perspective and thoughtful analysis have made this a truly engrossing read. I’m thankful for the effort she has put into creating such an informative and thought-provoking piece. Thank you, author, for sharing your wisdom and sparking meaningful discussions through your exceptional writing!
Have you ever thought about including a little bit more than just your articles? I mean, what you say is fundamental and everything. But think of if you added some great visuals or video clips to give your posts more, “pop”! Your content is excellent but with images and videos, this blog could certainly be one of the most beneficial in its field. Wonderful blog!
canadian drugs: usa canada pharm – usa canada pharm
Outstanding post however , I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit more. Cheers!
http://usacanadapharm.com/# usa canada pharm
As I website possessor I believe the content matter here is rattling excellent , appreciate it for your hard work. You should keep it up forever! Good Luck.
I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are wonderful! Thanks!
northwest pharmacy canada USACanadaPharm canada pharmacy
I really enjoy the post.Really thank you!
It’s a pity you don’t have a donate button! I’d certainly donate to this fantastic blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this website with my Facebook group. Talk soon!
usa canada pharm: canadian pharmacy phone number – USACanadaPharm
obviously like your website but you need to check the spelling on several of your posts. Many of them are rife with spelling problems and I to find it very bothersome to tell the truth nevertheless I?ll definitely come back again.
Hello, you used to write wonderful, but the last several posts have been kinda boring? I miss your tremendous writings. Past few posts are just a little out of track! come on!
https://usacanadapharm.shop/# canada pharmacy online