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://sildenafilit.pro/# alternativa al viagra senza ricetta in farmacia
farmacie online affidabili
viagra originale in 24 ore contrassegno: viagra prezzo – viagra 100 mg prezzo in farmacia
farmacia online senza ricetta BRUFEN 600 prezzo in farmacia comprare farmaci online con ricetta
7к казино отзывы 7к казино отзывы .
farmacie online autorizzate elenco farmacia online piГ№ conveniente comprare farmaci online all’estero
viagra naturale: acquisto viagra – kamagra senza ricetta in farmacia
Сервисный центр предлагает срочный ремонт робота пылесоса anker ремонт робота пылесоса anker недорого
acquistare farmaci senza ricetta: farmacia online migliore – Farmacia online piГ№ conveniente
сколько стоит ремонт кондиционера
Hi there, simply changed into aware of your weblog via Google, and located that it’s really informative. I am going to be careful for brussels. I will be grateful if you happen to proceed this in future. Many other people will likely be benefited from your writing. Cheers!
http://tadalafilit.com/# Farmacia online piГ№ conveniente
farmacie online sicure
Farmacie online sicure: Farmacia online migliore – farmacia online senza ricetta
Furthermore, i believe that mesothelioma cancer is a rare form of cancer malignancy that is often found in those people previously subjected to asbestos. Cancerous tissue form while in the mesothelium, which is a shielding lining that covers most of the body’s internal organs. These cells normally form in the lining of your lungs, tummy, or the sac which encircles the heart. Thanks for revealing your ideas.
Hi, Neat post. There’s a problem with your website in internet explorer, would check this? IE still is the market leader and a big portion of people will miss your great writing because of this problem.
One thing I would really like to say is that before obtaining more pc memory, check out the machine in to which it will be installed. When the machine is definitely running Windows XP, for instance, the particular memory ceiling is 3.25GB. Installing over this would purely constitute a waste. Make sure one’s motherboard can handle your upgrade volume, as well. Great blog post.
Hey there! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
There are certainly a variety of details like that to take into consideration. That could be a great point to carry up. I supply the thoughts above as common inspiration however clearly there are questions just like the one you bring up where the most important factor might be working in trustworthy good faith. I don?t know if best practices have emerged around things like that, however I’m positive that your job is clearly identified as a good game. Both girls and boys feel the impact of only a moment?s pleasure, for the rest of their lives.
Thanks for your useful article. One other problem is that mesothelioma cancer is generally caused by the inhalation of materials from asbestos fiber, which is a very toxic material. It really is commonly found among staff in the construction industry who may have long contact with asbestos. It’s also caused by residing in asbestos insulated buildings for a long time of time, Inherited genes plays a crucial role, and some people are more vulnerable on the risk as compared with others.
I have been exploring for a little bit for any high-quality articles or weblog posts on this kind of house . Exploring in Yahoo I ultimately stumbled upon this website. Studying this info So i?m happy to show that I have an incredibly excellent uncanny feeling I came upon just what I needed. I so much certainly will make sure to do not omit this web site and give it a look regularly.
comprare farmaci online all’estero farmacia online piГ№ conveniente farmaci senza ricetta elenco
acquisto farmaci con ricetta Farmacie on line spedizione gratuita Farmacia online miglior prezzo
discountshoesmart sk364
Fake Shoes,Replica Sneaker as776
Farmacia online piГ№ conveniente Farmacie on line spedizione gratuita п»їFarmacia online migliore
I have read several good stuff here. Definitely value bookmarking for revisiting. I wonder how much attempt you set to make the sort of magnificent informative website.
I am really inspired together with your writing talents as neatly as with the layout for your weblog. Is that this a paid subject or did you modify it your self? Either way keep up the nice high quality writing, it?s rare to look a nice blog like this one nowadays..
Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.
comprare farmaci online con ricetta Farmacie on line spedizione gratuita Farmacie online sicure
F*ckin? tremendous things here. I?m very glad to see your post. Thanks a lot and i am looking forward to contact you. Will you please drop me a mail?
Отличный сайт! Всем рекомендую!винтовые сваи заказать спб
Howdy would you mind stating which blog platform you’re using? I’m looking to start my own blog in the near future but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S Sorry for being off-topic but I had to ask!