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 } }
наркологическая срочная помощь наркологическая срочная помощь .
наркологическая скорая в москве наркологическая скорая в москве .
There are various gadgets which are crucial to stylish women, and gold chain is certainly one of them.
срочное изготовление загранпаспорта срочное изготовление загранпаспорта .
загранпаспорт оформить загранпаспорт оформить .
http://fastpillseasy.com/# ed medication online
where can i get ed pills cheap cialis ed pills cheap
Nie insists that regardless of the high common cost of a casket, most funeral houses carry a range of caskets to meet the price range of everybody of their group.
Regenerating treads aren’t the only way to make tires last longer.
1.e4 e5 2.Nc3 Nf6 3.Bc4 Nxe4 4.Nxe4 Allowing Black quick equality, at least.
A trader is a hedger when they go short on futures contracts while owning the basic asset or other futures contracts of the same or related underlying in order to protect their existing positions opposite price fluctuations.
online erectile dysfunction generic ed meds online ed doctor online
De Gaulle believed that he could persuade the Vichy French forces in Dakar to affix the Allies.
наркологическая скорая помощь москва наркологическая скорая помощь москва .
вызвать наркологическую помощь http://justforum.bestforums.org/viewtopic.php?f=26&t=4785/ .
как вызвать наркологическую скорую помощь в москве как вызвать наркологическую скорую помощь в москве .
Is the current use the highest and best use or should improvements be demolished (explain if used as improved; land and demolition; highest and best use is demolition).
Steer clear of Mokha Packing which is in home and as an alternative have the local UPS Store choose up objects and pack then ship.
https://fastpillsformen.com/# Cheap generic Viagra online
The workforce from the experienced and expert Occasion Administration Companies in Delhi offers numerous events that will definitely add kudos in direction of your group.
Whether you buy a new or used car, car dealer fees can be very high, so let’s see which ones you may be able to avoid so you can save money.
Army rites have been in the Duffield Cemetery in Jay by the Grove American Legion Post.
диплом о среднем специальном образовании купить
Incomes that are generated inside these foreign bank accounts are reported on the person’s individual tax return on the year the income is earned.
Program inspectors are skilled to prepare and conduct a boater schooling program in their community.
The schedule for the 3-day occasion on Might 25-27 featured a handcycling time path and various different cycling time trials and road races, including a males’s road race that took the cyclists via the guts of downtown Chattanooga and up Lookout Mountain for a complete race distance of 102.5 miles (165.Zero km).
Also included is a topical index, a listing of Christian organizations and ministries, a gospel overview, a daily Bible verse, and a blog.
The Government of India is going to change the tax regime from April 2012 that will take out any equity linked tax saving schemes.
Buy Tadalafil 20mg: MaxPillsForMen.com – Tadalafil Tablet
Sildenafil 100mg price: FastPillsForMen – buy Viagra over the counter