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 } }
semaglutide cost semaglutide tablets semaglutide tablets
I liked up to you’ll receive carried out right here. The cartoon is tasteful, your authored subject matter stylish. nevertheless, you command get got an shakiness over that you would like be turning in the following. sick without a doubt come more before once more since exactly the similar just about very often within case you defend this increase.
With havin so much written content do you ever run into any issues of plagorism or copyright violation? My website has a lot of exclusive content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any ways to help reduce content from being ripped off? I’d certainly appreciate it.
buy ozempic: buy ozempic pills online – buy ozempic
I would like to thnkx for the efforts you have put in writing this site. I am hoping the same high-grade website post from you in the upcoming as well. In fact your creative writing skills has inspired me to get my own website now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.
https://ozempic.art/# buy cheap ozempic
Thanks for your beneficial post. As time passes, I have come to be able to understand that the actual symptoms of mesothelioma cancer are caused by your build up associated fluid between the lining on the lung and the upper body cavity. The ailment may start while in the chest vicinity and multiply to other body parts. Other symptoms of pleural mesothelioma cancer include weight reduction, severe deep breathing trouble, temperature, difficulty taking in food, and inflammation of the face and neck areas. It really should be noted that some people having the disease will not experience virtually any serious indications at all.
Профессиональный сервисный центр по ремонту автомагнитол в Москве.
Мы предлагаем: сервис центр ремонта магнитол
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
wonderful points altogether, you simply gained a new reader. What would you suggest in regards to your post that you made some days ago? Any positive?
Профессиональный сервисный центр по ремонту сигвеев в Москве.
Мы предлагаем: ремонт сигвей в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Valuable information. Lucky me I found your web site by accident, and I am shocked why this accident did not happened earlier! I bookmarked it.
permethrin canada – buy generic benzac over the counter buy tretinoin gel generic
Hi, i think that i saw you visited my weblog thus i came to ?return the favor?.I am trying to find things to enhance my website!I suppose its ok to use a few of your ideas!!
ozempic cost: buy ozempic – ozempic cost
buy semaglutide pills buy semaglutide pills buy rybelsus online
buy cheap ozempic: buy ozempic – buy ozempic
https://rybelsus.shop/# semaglutide cost
Hi there would you mind stating which blog platform you’re working with? I’m planning to start my own blog in the near future but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!
This design is incredible! You certainly know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it. Too cool!
very good post, i definitely love this web site, keep on it
I believe that avoiding refined foods is the first step for you to lose weight. They will taste excellent, but ready-made foods possess very little vitamins and minerals, making you take in more in order to have enough vigor to get through the day. Should you be constantly ingesting these foods, transitioning to whole grain products and other complex carbohydrates will let you have more electricity while consuming less. Thanks alot : ) for your blog post.
I do not even know how I finished up here, but I believed this put up was once great. I don’t know who you might be but certainly you’re going to a famous blogger when you are not already 😉 Cheers!
Great beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea
http://ozempic.art/# buy cheap ozempic
cheapest rybelsus pills: rybelsus pill – semaglutide tablets
buy ozempic buy ozempic pills online ozempic online
http://rybelsus.shop/# rybelsus price
Great blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. Thank you
Its such as you learn my thoughts! You seem to understand so much approximately this, such as you wrote the ebook in it or something. I think that you simply can do with some to drive the message home a bit, but other than that, that is magnificent blog. A great read. I will certainly be back.
ozempic cost: Ozempic without insurance – Ozempic without insurance