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 } }
For starters, by no means retailer dirty instruments.
Read on to learn more about introducing new traditions to your family.
We passed refugee columns, the carts abandoned by their homeowners, who had fled in panic into the fields.
Future Assist Financial Services Group Pty Ltd does not give any guarantee of accuracy, nor accept any accountability for errors or omissions on this doc.
It should be a sporty notchback and/or fastback coupe; the convertible is dead and will be forgotten.
After his parole, and between his numerous prison terms, Whitaker turned once again a tournament chess participant, and for several years was some of the energetic aggressive players in America and Europe.
Much less wanted to tell her household’s story to help honor her dad, and to remind everybody in regards to the threat of West Nile virus.
In keeping with Yeager, financial-disequilibrium is part of the monetarist tradition which states that “cash matters the most” which can’t be true as when it comes to financial analysis actors matter most.
Value charts on bond funds sometimes do not reflect their efficiency due to the lack of yield consideration.
When interviewing florist candidates, find out how open they are to working within your price range.
Doing business with Naira4all is an effective one because they keep to promise and reply to buyer request on time..
These are also made in clean Italian leather from certified tanneries with a grungy type.
They carried out nicely on both dry and wet surfaces and offered enough traction to maintain us from slipping or shedding our balance.
Musical displays all through the week will stability the volleyball to pleasure and amuse all present there.
prednisone 10 Predni Best prednisone in canada
Wow! This could be one particular of the most useful blogs We’ve ever arrive across on this subject. Actually Fantastic. I am also a specialist in this topic so I can understand your effort.
These modifications in inventory market can impact our complete return indices as these are the measure of our overall portfolio returns.
Higher Pittston is a 65.35 sq mi (169.3 km2) area in Luzerne County, Pennsylvania, encompassing the Susquehanna River.
By following these simple ideas, you possibly can construct and maintain fulfilling and long-lasting platonic friendships that enrich your life in numerous ways.
Within the course of only 15 years the number of sponsorships has increased to 2,500.
We’ve the marriage decorator who patiently listens to your wants and even give strategies; they’re extremely skilled and expert with the intention to take pleasure in your wedding problem-free.
In April 2012, Zurich Financial Services Ltd modified back to its authentic title, Zurich Insurance coverage Group Ltd.
The time period “task lighting” has noticeably popped up within the vernacular since house renovations have change into all the craze.
Can I just say what a reduction to seek out someone who actually knows what theyre speaking about on the internet. You definitely know how to carry a difficulty to gentle and make it important. Extra folks have to read this and perceive this facet of the story. I cant consider youre not more well-liked since you positively have the gift.
Bartram’s Coated Bridge (1860) – the final remaining covered bridge in Delaware County (spanning Crum Creek – connecting with Chester County), positioned at Goshen and Boot roads.
These crops are continuously produced in giant quantities and ceaselessly require specialised instruments or substances to extend output.
Brokers and sellers would do well to avail themselves of its providers.
That fact should have legal professionals licking their lips in anticipation and criminals rubbing their temples with fear.
A tiny island like that, admittedly, is easy to lose sight of way out within the Pacific, some 3,800 miles (6,one hundred fifteen kilometers) west of Honolulu.
It’s the exclusion on foreign housing which permits the taxes payers to decide for extra exclusions on the idea of their income while paying some amount to cove the household bills as they use to live abroad.