java.time 包是在JDK8新引入的,提供了用於日期、時間、實例和週期的主要API。html
java.time包定義的類表示了日期-時間概念的規則,包括instants, durations, dates, times, time-zones and periods。這些都是基於ISO日曆系統,它又是遵循 Gregorian規則的。java
全部類都是不可變的、線程安全的。api
LocalDateTime:存儲了日期和時間,如:2013-10-15T14:43:14.539
。安全
LocalDate:存儲了日期,如:2013-10-15
。spa
LocalTime:存儲了時間,如:14:43:14.539
。.net
上面的類能夠由下面的類組合來:線程
類之間轉換的示例:
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime :" + localDateTime); LocalDate localDate = LocalDate.now(); System.out.println("localDate :" + localDate); LocalTime localtime = LocalTime.now(); System.out.println("localtime :" + localtime); // 獲取當前年份 Year year = Year.now(); System.out.println("year :" + year); // 從Year獲取LocalDate LocalDate localDate1 = year.atDay(59); System.out.println("localDate1 :" + localDate1); // 把LocalTime關聯到一個LocalDate獲得一個LocalDateTime LocalDateTime localDateTime1 = localtime.atDate(localDate1); System.out.println("localDateTime1 :" + localDateTime1); // 用指定的年獲取一個Year Year year1 = Year.of(2012); System.out.println("year1 :" + year1); // 從Year獲取YearMonth YearMonth yearMonth = year1.atMonth(2); System.out.println("yearMonth :" + yearMonth); // YearMonth指定日獲得 LocalDate LocalDate localDate2 = yearMonth.atDay(29); System.out.println("localDate2 :" + localDate2); // 判斷是不是閏年 System.out.println("isLeapYear :" + localDate2.isLeapYear()); //自動處理閏年的2月日期 //建立一個 MonthDay MonthDay monthDay = MonthDay.of(2, 29); LocalDate leapYear = monthDay.atYear(2012); System.out.println("leapYear :" + leapYear); //同一個 MonthDay 關聯到另外一個年份上 LocalDate nonLeapYear = monthDay.atYear(2011); System.out.println("nonLeapYear :" + nonLeapYear);
上面代碼的輸出結果爲:
localDateTime :2013-10-15T15:11:57.489 localDate :2013-10-15 localtime :15:11:57.489 year :2013 localDate1 :2013-02-28 localDateTime1 :2013-02-28T15:11:57.489 year1 :2012 yearMonth :2012-02 localDate2 :2012-02-29 isLeapYear :true leapYear :2012-02-29 nonLeapYear :2011-02-28
DateTimeFormatter:在日期對象與字符串之間進行轉換。
ChronoUnit:計算出兩個時間點之間的時間距離,可按多種時間單位計算。
TemporalAdjuster:各類日期計算功能。
續前面的代碼:
DayOfWeek dayOfWeek = DayOfWeek.of(1); System.out.println("dayOfWeek :" + dayOfWeek); //計算兩個日期之間的天數,還能夠按其餘時間單位計算兩個時間點之間的間隔。 long between = ChronoUnit.DAYS.between(localDate, leapYear); System.out.println("between :" + between); // 線程安全的格式化類,不用每次都new個SimpleDateFormat DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu MM dd"); // 把日期時間轉換爲字符串標識 System.out.println("date formatter :" + dateTimeFormatter.format(nonLeapYear)); // 解析字符串形式的日期時間 TemporalAccessor temporalAccessor = dateTimeFormatter.parse("2013 01 15"); System.out.println("temporalAccessor :" + LocalDate.from(temporalAccessor)); Instant instant = Instant.now(); // 時間戳 System.out.println("instant :" + instant); //計算某月的第一天的日期 LocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth()); System.out.println("with :" + with); // 計算某月的第一個星期一的日期 TemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY); LocalDate with1 = localDate.with(temporalAdjuster); System.out.println("with1 :" + with1); // 計算localDate的下一個星期一的日期 LocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY)); System.out.println("with2 :" + with2);
輸出:
dayOfWeek :MONDAY between :-594 date formatter :2011 02 28 temporalAccessor :2013-01-15 instant :2013-10-15T07:55:30.964Z with :2011-02-01 with1 :2013-10-07 with2 :2013-10-21
轉換可經過下面的方法進行。
Date.toInstant() Date.from(Instant) Calendar.toInstant()
該包的API提供了大量相關的方法,這些方法通常有一致的方法前綴:
of:靜態工廠方法。
parse:靜態工廠方法,關注於解析。
get:獲取某些東西的值。
is:檢查某些東西的是不是true。
with:不可變的setter等價物。
plus:加一些量到某個對象。
minus:從某個對象減去一些量。
to:轉換到另外一個類型。
at:把這個對象與另外一個對象組合起來,例如:date.atTime(time)
。
想起之前作報表都是在存儲過程裏處理日期的,由於Java的日期操做確實太弱了。有了Java 8,媽媽不再用擔憂我處理日期了。