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 } }
gates of olympus oyna gates of olympus giris gates of olympus demo turkce oyna
Between January 1, 1963, and December 31, 2014, 1,186 index parts have been replaced by different parts.
Whereas the CEO, Michael O’Leary, remained adamant that state assist was not an choice, the carrier announced several modifications to its operations.
betine betine sikayet betine guncel
Yet another issue is that video gaming became one of the all-time main forms of excitement for people of any age. Kids have fun with video games, and also adults do, too. The XBox 360 is amongst the favorite games systems for folks who love to have hundreds of games available to them, plus who like to relax and play live with people all over the world. Thanks for sharing your opinions.
Booming industrial sector – The business sectors within the nation are displaying phenomenal progress since the last few years that have actually added a boost to the financial system of the country.
<a href=”https://remont-kondicionerov-wik.ru”>кондиционер ремонт</a>
When the housing bubble burst in 2008, the socialist Gordon Bajnai administration (2009-2010) centered on lowering public debt and deficit slightly than the personal aspect (over-indebted population).
I?ll right away grasp your rss feed as I can’t find your e-mail subscription link or newsletter service. Do you have any? Kindly let me know in order that I may just subscribe. Thanks.
Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
Makortoff, Kalyeena (18 February 2020).
It was tied to a 4-speed automatic; this transmission was non-obligatory on the V-6.
Bhat, J. M.; Gupta, M.; Murthy, S. N. (September 2006).
Thanks for your article on this site. From my very own experience, there are times when softening right up a photograph may provide the photo shooter with a chunk of an creative flare. More often than not however, this soft blur isn’t just what exactly you had under consideration and can often times spoil an otherwise good snapshot, especially if you intend on enlarging that.
Sturdy and destructive tornadoes kind most frequently in the United States, Argentina, Brazil, Bangladesh, and Jap India, but can occur virtually wherever underneath the fitting circumstances.
Howdy! Do you use Twitter? I’d like to follow you if that would be ok. I’m absolutely enjoying your blog and look forward to new updates.
Their larger means to influence company behaviour as nicely to select investors profiles could assist diminish company costs.
The underneath-development Ocean Flower Island in Hainan is considered one of its major projects.
What?s Happening i’m new to this, I stumbled upon this I’ve found It positively useful and it has helped me out loads. I hope to contribute & help other users like its helped me. Great job.
https://starzbet.shop/# starz bet giris
http://starzbet.shop/# starzbet guncel giris
ручные листогибы ручные листогибы .
Great site! I recommend it to everyone!tarjima kinolar
Hi there! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains. If you know of any please share. Cheers!
This network aims to assist people arrange their CSAs, in addition to offering helpful info to assist these relations to run extra successfully.
Youre so cool! I dont suppose Ive learn anything like this before. So nice to search out someone with some authentic thoughts on this subject. realy thanks for beginning this up. this web site is one thing that’s needed on the internet, somebody with somewhat originality. useful job for bringing one thing new to the web!
I have observed that in the world the present day, video games include the latest trend with children of all ages. Many times it may be extremely hard to drag your children away from the video games. If you want the very best of both worlds, there are various educational video games for kids. Great post.
We’re a group of volunteers and starting a new scheme in our community. Your site provided us with valuable info to work on. You have done an impressive job and our entire community will be thankful to you.
Generally I don’t read article on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, very nice post.
https://betine.online/# betine guncel giris