Java 8 Time API( 三 )

注意:以上例子通过输入参数创建实例时,如果输入了无效的参数name将会抛出java.time.DateTimeException
4.Instantinstant类用于处理机器可读的时间格式 。instant类将日期时间存储在unix时间戳中 。
// 当期时间戳Instant timestamp = Instant.now();System.out.println("当期时间戳= "+timestamp);// 从纪元日开始的第多少毫秒Instant specificTime = Instant.ofEpochMilli(timestamp.toEpochMilli());System.out.println("从纪元日开始="+specificTime);运行之后结果如下:
当期时间戳=2022-10-26T08:08:40.429Z从纪元日开始=2022-10-26T08:08:40.429ZJava8日期时间API类的实用方法大多数日期时间类都会提供各种实用方法,例如加/减天数、周数、月数等 。还有一些其他实用方法可以使用时间调整器TemporalAdjuster调整日期,并计算两个日期之间的时间段 。
LocalDate today = LocalDate.now();//获取年份,判断年份是否是闰年System.out.println("Year "+today.getYear()+" is Leap Year? "+today.isLeapYear());//比较两个时间System.out.println("Today is before 01/01/2023? "+today.isBefore(LocalDate.of(2023,1,1)));//通过LocalDate创建LocalDateTimeSystem.out.println("Current Time="+today.atTime(LocalTime.now()));//加减操作System.out.println("10 days after today will be "+today.plusDays(10));System.out.println("3 weeks after today will be "+today.plusWeeks(3));System.out.println("20 months after today will be "+today.plusMonths(20));System.out.println("10 days before today will be "+today.minusDays(10));System.out.println("3 weeks before today will be "+today.minusWeeks(3));System.out.println("20 months before today will be "+today.minusMonths(20));//时间调整器调整时间System.out.println("First date of this month= "+today.with(TemporalAdjusters.firstDayOfMonth()));LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());System.out.println("Last date of this year= "+lastDayOfYear);Period period = today.until(lastDayOfYear);System.out.println("Period Format= "+period);System.out.println("Months remaining in the year= "+period.getMonths());运行之后结果如下:
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

经验总结扩展阅读