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】

推荐阅读