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 } }
whoah this blog is magnificent i like reading your articles. Stay up the great work! You know, lots of persons are looking around for this info, you can aid them greatly.
Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you
Hi, Neat post. There’s an issue together with your site in web explorer, may test this? IE still is the market chief and a big component to people will miss your excellent writing because of this problem.
Kamagra Gel: Kamagra kaufen ohne Rezept – Kamagra Oral Jelly kaufen
https://kamagrapotenzmittel.shop/# Kamagra online bestellen
Whats up are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you require any coding knowledge to make your own blog? Any help would be really appreciated!
Hello, i think that i saw you visited my blog so i came to ?return the favor?.I am attempting to find things to enhance my website!I suppose its ok to use some of your ideas!!
There are definitely numerous particulars like that to take into consideration. That could be a great point to deliver up. I provide the thoughts above as normal inspiration however clearly there are questions like the one you convey up where an important thing can be working in sincere good faith. I don?t know if greatest practices have emerged round things like that, however I’m sure that your job is clearly recognized as a good game. Both girls and boys really feel the impression of only a second?s pleasure, for the rest of their lives.
https://apotekonlinerecept.shop/# apotek pa nett
At this time it appears like Drupal is the best blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?
Very nice post. I just stumbled upon your blog and wanted to say that I’ve really enjoyed surfing around your blog posts. After all I will be subscribing to your feed and I hope you write again soon!
I was recommended this blog via my cousin. I’m no longer sure whether or not this post is written by way of him as no one else recognise such targeted approximately my trouble. You’re wonderful! Thanks!
http://apotekonlinerecept.com/# apotek pa nett
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем:сервис центры бытовой техники москва
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Hiya, I am really glad I’ve found this information. Nowadays bloggers publish just about gossips and net and this is really irritating. A good web site with interesting content, this is what I need. Thank you for keeping this website, I will be visiting it. Do you do newsletters? Can’t find it.
Betrouwbare online apotheek zonder recept: online apotheek – online apotheek
apotek online recept: Apotek hemleverans recept – Apotek hemleverans idag
I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks!
Kamagra Original Kamagra Oral Jelly Kamagra Oral Jelly kaufen
Apotheek online bestellen: Apotheek Max – Betrouwbare online apotheek zonder recept
I am not certain the place you are getting your info, but good topic. I must spend some time finding out much more or figuring out more. Thank you for great information I used to be in search of this information for my mission.
Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is magnificent, as well as the content!
http://kamagrapotenzmittel.com/# Kamagra Original
A different issue is really that video gaming has become one of the all-time largest forms of entertainment for people of nearly every age. Kids have fun with video games, and also adults do, too. The XBox 360 is one of the favorite games systems for folks who love to have a huge variety of video games available to them, in addition to who like to experiment with live with others all over the world. Many thanks for sharing your opinions.
I think one of your adverts triggered my web browser to resize, you might want to put that on your blacklist.
You made some decent points there. I regarded on the web for the difficulty and found most individuals will associate with together with your website.
Thanks for making me to get new thoughts about computers. I also possess the belief that certain of the best ways to maintain your laptop in perfect condition is with a hard plastic-type material case, or even shell, that suits over the top of your computer. A majority of these protective gear are model precise since they are made to fit perfectly above the natural casing. You can buy these directly from owner, or from third party places if they are for your notebook, however don’t assume all laptop may have a spend on the market. Again, thanks for your recommendations.
Kamagra kaufen ohne Rezept: Kamagra online bestellen – Kamagra online bestellen
Hey there, You have done an incredible job. I will certainly digg it and personally recommend to my friends. I’m sure they will be benefited from this site.
I’m not sure why but this weblog is loading incredibly slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later on and see if the problem still exists.