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://zithropharm.shop/# how much is zithromax 250 mg
zithromax without prescription: zithromax pill – zithromax antibiotic
https://zithropharm.com/# zithromax for sale cheap
buy augmentin tablets – order amoxiclav generic purchase duloxetine pills
Astherus: Your Partner in Decentralized Finance Innovation
Astherus offers a groundbreaking platform that combines blockchain technology with powerful financial tools. Whether you’re a seasoned investor or new to the world of DeFi, Astherus provides a secure, transparent, and efficient solution for managing assets. https://astherus.org
Why Astherus?
Trustworthy Technology: Blockchain ensures transparency and security.
Innovative Features: Advanced tools tailored for DeFi users.
User-Centric Design: Accessible, intuitive, and adaptable to all needs.
Discover the next generation of decentralized finance with Astherus!
amoxicillin canada price: amoxicillin order online – amoxicillin 500 coupon
On the whole, vivid and brushed/antiqued brass hardware are thought of elegant traditional, nickel-finish steel is traditional, wrought iron or wooden is rustic conventional, and shiny chrome and enameled steel are trendy.
can i order clomid tablets: ClmHealthPharm – buy cheap clomid price
Why MachFi is a Game Changer in DeFi.
With MachFi, DeFi on the Sonic Chain reaches new heights. Our unique borrow-lending platform allows users to create custom trading strategies that suit their needs and optimize performance. visit to https://machfi.net/
Why MachFi?
– Security: Built on the Sonic Chain’s robust blockchain technology.
– Flexibility: Custom strategies for lending and borrowing.
– Efficiency: Fast, reliable transactions with lower fees.
Experience the next generation of DeFi with MachFi.
amoxicillin 500 capsule: where to get amoxicillin over the counter – can you purchase amoxicillin online
can i purchase generic clomid without prescription: where buy clomid without a prescription – how to buy cheap clomid now
how to get cheap clomid tablets: ClmHealthPharm – cost of generic clomid tablets
can i purchase clomid: ClmHealthPharm – how can i get generic clomid without insurance
where buy cheap clomid without prescription: where buy clomid – where can i get generic clomid no prescription
buy doxycycline 100 mg tablet: DoxHealthPharm – doxycycline 200 mg pill
zithromax 500: zithromax for sale online – zithromax for sale us
https://clmhealthpharm.com/# cheap clomid pill
Com o codigo promocional 1XBET: GRATIS777 voce recebe bonus de boas-vindas de ate R$1200 para fazer suas apostas esportivas. Use o codigo no cadastro para liberar beneficios em grandes eventos de futebol e outras vantagens.
1xbet codigo promocional brasil
https://clmhealthpharm.shop/# where can i get cheap clomid without dr prescription
amoxicillin online without prescription amoxicillin 500mg price purchase amoxicillin online without prescription
Com o codigo promocional 1XBET: GRATIS777 voce recebe bonus de boas-vindas de ate R$1200 para fazer suas apostas esportivas. Use o codigo no cadastro para liberar beneficios em grandes eventos de futebol e outras vantagens.
codigo promocional 1xbet mexico
The special results department needed to produce two very completely different Manhattan landscapes and multiple superpower effects for “Five Years Gone.” It also had to depict Nathan Petrelli (Adrian Pasdar) in front of the White House and at Floor Zero earlier than having him fly across the new York skyline.
zithromax online usa no prescription: buy cheap generic zithromax – generic zithromax azithromycin
doxycycline price south africa: doxycycline tablet – doxycycline 40 mg india
can you buy zithromax over the counter in canada Zithro Pharm zithromax prescription online
can i order cheap clomid pills: ClmHealthPharm – where can i get cheap clomid without prescription
amoxicillin azithromycin Amo Health Pharm amoxicillin discount
https://amohealthpharm.com/# cost of amoxicillin 30 capsules
how to get zithromax online: zithromax 500 mg – zithromax online usa no prescription
Wow, fantastic blog format! How long have you been blogging for? you make blogging glance easy. The entire look of your site is fantastic, as well as the content!