概述

Java 8 日期,時間常用操作及格式化。
重點怎麼把時間戳轉換為帶有時區的時間字元串?
帶有時區的字元串怎樣轉換為時間?
Java 8中時間,日期 與Date, Calendar 怎樣互轉?
Java 6,7中想使用Java 8中的日期處理,應該怎麼解決?
Chrono Units Enum
一套标準的日期期間機關。
這套單元提供基于單元的通路來操作日期、時間或日期時間。機關的标準可以通過實施擴充
1. LocalDate date = LocalDate.now(); 2. System.out.println("current date is :" + 3. date); 4. // adding 2 years to the current date 5. LocalDate year = 6. date.plus(2, ChronoUnit.YEARS); 7. 8. System.out.println("next to next year is " + 9. year); 10. // adding 1 month to the current data 11. LocalDate nextMonth = 12. date.plus(1, ChronoUnit.MONTHS); 13. 14. System.out.println("the next month is " + 15. nextMonth); 16. // adding 1 week to the current date 17. LocalDate nextWeek = 18. date.plus(1, ChronoUnit.WEEKS); 19. 20. System.out.println("next week is " + nextWeek); 21. 22. // adding 2 decades to the current date 23. LocalDate Decade = 24. date.plus(2, ChronoUnit.DECADES); 25. 26. System.out.println("20 years after today " + 27. Decade);
輸出結果
1. current date is :2018-04-09 2. next to next year is 2020-04-09 3. the next month is 2018-05-09 4. next week is 2018-04-16 5. 20 years after today 2038-04-09
Temporal Adjusters
包含常用日期擷取常量參數。
1. LocalDate date = LocalDate.now(); 2. System.out.println("the current date is "+ 3. date); 4. 5. // to get the first day of next month 6. LocalDate dayOfNextMonth = 7. date.with(TemporalAdjusters. 8. firstDayOfNextMonth()); 9. 10. System.out.println("firstDayOfNextMonth : " + 11. dayOfNextMonth ); 12. 13. // get the next saturday 14. LocalDate nextSaturday = 15. date.with(TemporalAdjusters. 16. next(DayOfWeek.SATURDAY)); 17. 18. System.out.println("next satuday from now is "+ 19. nextSaturday); 20. 21. // first day of current month 22. LocalDate firstDay = 23. date.with(TemporalAdjusters. 24. firstDayOfMonth()); 25. 26. System.out.println("firstDayOfMonth : " + 27. firstDay); 28. 29. // last day of current month 30. LocalDate lastDay = 31. date.with(TemporalAdjusters. 32. lastDayOfMonth()); 33. 34. System.out.println("lastDayOfMonth : " + 35. lastDay);
常用方法
如果上面表格中列出的方法不能滿足你的需求,你還可以建立自定義的TemporalAdjuster接口的實作,TemporalAdjuster也是一個函數式接口,是以我們可以使用Lambda表達式:
@FunctionalInterfacepublic interface TemporalAdjuster { Temporal adjustInto(Temporal temporal);}
計算下個工作日,跳過周六,周天。
1. LocalDate date = LocalDate.of(2019, 10, 11); 2. LocalDateTime localDateTime2 = date.with(temporal -> { 3. // 目前日期 4. DayOfWeek dayOfWeek = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); 5. 6. // 正常情況下,每次增加一天 7. int dayToAdd = 1; 8. 9. // 如果是星期五,增加三天 10. if (dayOfWeek == DayOfWeek.FRIDAY) { 11. dayToAdd = 3; 12. } 13. 14. // 如果是星期六,增加兩天 15. if (dayOfWeek == DayOfWeek.SATURDAY) { 16. dayToAdd = 2; 17. } 18. 19. return temporal.plus(dayToAdd, ChronoUnit.DAYS); 20. }).atStartOfDay(); 21. System.out.println(localDateTime2); 22. //結果 23. 2019-10-14T00:00
與舊日期相容
Date, Calendar通過Instant轉換為LocalDateTime
1. Date date = new Date(); 2. Calendar calendar = Calendar.getInstance(); 3. LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); 4. System.out.println("date to localDateTime "+localDateTime); 5. LocalDateTime localDateTime1= LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault()); 6. System.out.println("calendar to localDateTime "+localDateTime1);
Instant 轉化為 Date,Calendar
Instant 轉換Date可以直接通過from 方法建構。
Instant 轉換 Calendar,先轉換ZonedDateTime,在使用GregorianCalendar from 方法。
1. Instant instant = Instant.now(); 2. Date myDate = Date.from(instant); 3. ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); 4. Calendar cal1 = GregorianCalendar.from(zdt);
日期格式化
新的日期API中提供了一個DateTimeFormatter類用于處理日期格式化操作,它被包含在java.time.format包中,Java 8的日期類有一個format()方法用于将日期格式化為字元串,該方法接收一個DateTimeFormatter類型參數:
1. LocalDateTime dateTime = LocalDateTime.now(); 2. String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); 3. System.out.println("BASIC_ISO_DATE time"+strDate1); 4. String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); 5. System.out.println("ISO_LOCAL_DATE time"+strDate2); 6. String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); 7. System.out.println("ISO_LOCAL_TIME time"+strDate3); 8. String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); 9. System.out.println("yyyy-MM-dd time"+strDate4); 10. String strDate5 = dateTime.format(DateTimeFormatter.ofPattern("今天是:yyyy年 MM月 dd日