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 } }
These are good looking shoes, particularly while on the airport or in transit.
However some instances and some moments appear more auspicious than others.
Okay, we’ve lined a great amount of sneakers which can be greatest for pleased, sunny, and heat weather, however how about when rain is within the forecast?
Wearing boots as your go-to footwear for journey can even add heat and warmth to your body as a result of the added supplies and the best way they are made are largely designed to maintain our body temperature warm in the winter season.
Later, the Professor places the dagger in Rosita’s bedroom to protect her.
Footwear take up a lot of beneficial area in luggage, in spite of everything, so you want to make each pair depend.
As a married man above the age of 35 years, he wasn’t subject to conscription, however in any occasion, provided that his actual enterprise concerned making clothes for males, it seems likely that he would have ended up making uniforms and related garments, and supplying necessities like warm underclothes, though I haven’t any precise evidence for this.
And though you can not compare it with a fabric materials, it allows a great move of air into the shoe.
With a cushioned footbed, it strikes the right stability between style and comfort.
The winter after shopping for the car Tom drove all the way down to Texas to pick up the brand new Monza physique he had purchased; several modifications had been wanted to get the physique to fit the chassis and meet the rule e-book for minimum width dimensions.
It can work so lengthy as it is handled as a single system with everyone on the identical page and that there aren’t “lacking hyperlinks” which break it.
UGG Traditional Minis are available varied colours, from traditional chestnut and black to more daring shades like pink and grey, allowing you to decide on a pair that best suits your fashion.
Perhaps it’s the lingering effects of the pandemic, or rising food-world inflation, however at a time when a date for 2 at many new Portland restaurants can cost upwards of $300 with tip, new carts seemed to redouble their deal with comfort meals, at a good worth.
I received off the L and headed north through McCarren Park until Bedford Avenue abruptly ended with no Rosemary’s in sight.
Now that your crystal is cleansed and charged, you’re able to solid spells.
One of the trendiest hair colors of the 12 months, ginger hair appears completely gorgeous with a long wolf haircut and multiple layers.
I made the tunics out of natural coloured cotton.
The restaurant has been a beloved establishment, serving conventional Pakistani delicacies which have stood the check of time.
When you’ve got a good friend or loved one captured by the Wordle phenomenon then this present is ideal.
Europe with these on in one day with no foot ache afterward.
Don’t think that these are spells to get you a job immediately.
Amish kitchen furniture is an effective alternative for a rustic-fashion culinary division.
Uber and Didi are both right here as well however never fully legalized their presence.
For those who journey to the cities in Ukraine or have a look on the women on Ukrainian dating sites, you should have a glance of them.
Alabama Development Corporations – AL Construction Company Listings.
Slightly than promoting out to a bigger, sounder institution, its managers gambled that the credit markets would rebound, arresting the fall in worth of tens of billions of dubious actual estate property festering on the firm’s steadiness sheet.
These spherical toe shoes have a suede lining that could be very comfy on your ft and the insole will also keep your ft comfy and dry.
Some are very simple and others a bit more superior.
These are known as grantor trusts.
Leonardville Methodist Episcopal Church (Leonardville, Kan.).