Java 8 Time API( 三 )

運行之后結果如下:
Year 2022 is Leap Year? falseToday is before 01/01/2023? trueCurrent Time=2022-10-26T16:25:04.74010 days after today will be 2022-11-053 weeks after today will be 2022-11-1620 months after today will be 2024-06-2610 days before today will be 2022-10-163 weeks before today will be 2022-10-0520 months before today will be 2021-02-26First date of this month= 2022-10-01Last date of this year= 2022-12-31Period Format= P2M5DMonths remaining in the year= 2Java8日期時間的解析和格式化經常用到的操作有:將日期時間格式化為不同格式String , 解析String以獲得日期時間對象 。
// 格式化LocalDate date = LocalDate.now();// 默認格式System.out.println("Default format of LocalDate=" + date);// 自定義格式System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));LocalDateTime dateTime = LocalDateTime.now();// 默認格式System.out.println("Default format of LocalDateTime=" + dateTime);// 自定義格式System.out.println(dateTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH時mm分ss秒")));System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));Instant timestamp = Instant.now();// 默認格式System.out.println("Default format of Instant=" + timestamp);// 解析LocalDateTime dt = LocalDateTime.parse("2022年10月24日10時24分24秒",DateTimeFormatter.ofPattern("yyyy年MM月dd日HH時mm分ss秒"));System.out.println("Default format after parsing = " + dt);運行之后結果如下:
Default format of LocalDate=2022-10-262022年10月26日20221026Default format of LocalDateTime=2022-10-26T16:37:51.3002022年10月26日16時37分51秒20221026Default format of Instant=2022-10-26T08:37:51.301ZDefault format after parsing = 2022-10-24T10:24:24對遺留日期時間的支持遺留日期/時間類幾乎在所有應用程序中都使用,因此必須有向下兼容 。這就是為什么我們可以通過一些實用方法將遺留類轉換為新類 , 反之亦然 。
//Date轉InstantInstant timestamp = new Date().toInstant();//Instant轉LocalDateTimeLocalDateTime date = LocalDateTime.ofInstant(timestamp,ZoneId.of(ZoneId.SHORT_IDS.get("CTT")));System.out.println("Date = " + date);//Calendar轉InstantInstant time = Calendar.getInstance().toInstant();System.out.println(time);//TimeZone轉ZoneIdZoneId defaultZone = TimeZone.getDefault().toZoneId();System.out.println(defaultZone);//ZonedDateTime from specific CalendarZonedDateTime gregorianCalendarDateTime = new GregorianCalendar().toZonedDateTime();System.out.println(gregorianCalendarDateTime);//Date API to Legacy classesDate dt = Date.from(Instant.now());System.out.println(dt);TimeZone tz = TimeZone.getTimeZone(defaultZone);System.out.println(tz);GregorianCalendar gc = GregorianCalendar.from(gregorianCalendarDateTime);System.out.println(gc);運行之后結果如下:
Date = 2022-10-26T16:47:38.3292022-10-26T08:47:38.429ZAsia/Shanghai2022-10-26T16:47:38.455+08:00[Asia/Shanghai]Wed Oct 26 16:47:38 CST 2022sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null]java.util.GregorianCalendar[time=1666774058455,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=31,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2022,MONTH=9,WEEK_OF_YEAR=43,WEEK_OF_MONTH=4,DAY_OF_MONTH=26,DAY_OF_YEAR=299,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=47,SECOND=38,MILLISECOND=455,ZONE_OFFSET=28800000,DST_OFFSET=0]可以看到,遺留的TimeZoneGregorianCalendartoString()方法過于冗長,對用戶不友好 。
【Java 8 Time API】

推薦閱讀