时间类
将 java.util.Date
设定为可变类型,以及 SimpleDateFormat
的非线程安全使其应用非常受限。之后就在 Java8 上添加了新特性。
java.time
中所有的类都是不可变对象。
Instant: 瞬时实例/时间戳。
LocalDate: 本地日期,不含具体时间。
LocalTime: 本地时间,不含具体日期。
LocalDateTime: 组合日期和时间,不包含时差和时间。
ZoneDateTime: 最完整的日期和时间。包含时区和相对于UTC的时差。
ZoneOffSet/ZoneId: 更为简单的操作时区。
DateTimeFormatter: 全新的时间格式化和解析类。
Year/Month: 只存储年月信息。
MonthDay/YearMonth: 记录年月/月日,用来指定特殊日期(如纪念日,到期月份)。
Clock: 获取当时的时间戳,或当前时区下的日期时间信息。
Period/Duration: 记录一段时间段,例如两个实例之间的时间差。
LocalDate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 LocalDate date = LocalDate.of(2020 , 10 , 1 ); LocalDate tempDate = LocalDate.now(); LocalDate date = LocalDate.from(LocalDateTime.now()); System.out.println(date.getYear()); System.out.println(date.getDayOfYear()); System.out.println(date.lengthOfYear()); System.out.println(date.isAfter(tempDate)); System.out.println(date.isLeapYear()); System.out.println(date.isEqual(tempDate)); System.out.println(date.isSupported(ChronoField.SECOND_OF_DAY)); System.out.println(date.minus(1 , ChronoUnit.DAYS)); System.out.println(date.minusDays(1 )); System.out.println(date.plusMonths(1 )); System.out.println(date.with(ChronoField.DAY_OF_YEAR, 45 )); System.out.println(date.withYear(2000 )); System.out.println(date.with(MonthDay.now())); System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy MM dd" ))); System.out.println(date.format(DateTimeFormatter.ISO_DATE)); LocalDate.parse("20201001" , DateTimeFormatter.ofPattern("yyyyMMdd" )); System.out.println(date.atStartOfDay()); System.out.println(date.atTime(1 ,1 ,1 ));
LocalTime
1 2 3 4 LocalTime time = LocalTime.now(); LocalTime time = LocalTime.of(12 , 12 , 12 ); time.withNano(12 );
LocalDateTime
1 2 3 LocalDateTime dateTime = LocalDateTime.now(); dateTime.toInstant(ZoneOffset.UTC); System.out.println(dateTime.toLocalDate());
Year/Month
1 2 3 4 5 6 7 Year year = Year.now(); Year year = Year.of(2000 ); Year year = Year.from(date); Month month = Month.of(1 ); Month month = date.getMonth();
YearMonth/MonthDay
1 2 3 4 5 YearMonth yearMonth = YearMonth.now(); YearMonth yearMonth = YearMonth.of(2000 , 10 ); YearMonth yearMonth = YearMonth.from(date);
Clock/Instant
1 2 3 4 5 6 7 8 9 10 11 12 Clock clock = Clock.systemUTC(); Clock clock = Clock.systemDefaultZone(); clock.withZone(ZoneId.of("Asia/Shanghai" )); clock.millis(); clock.instant(); Instant instant = Instant.now(); Instant instant = Instant.ofEpochMilli(1603380890080L );
ZoneId/ZoneOffSet
1 2 3 4 5 6 7 8 ZoneId zoneId = ZoneId.systemDefault(); ZoneId zoneId = ZoneId.of("Asia/Shanghai" ); ZoneOffset zoneOffset = ZoneOffset.of("+5" ); ZoneOffset zoneOffset = ZoneOffset.of("+05:30:30" ); zoneId.getRules(); zoneOffset.getTotalSeconds();
ZonedDateTime
1 2 3 4 5 6 7 8 ZonedDateTime zonedDateTime = ZonedDateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(2020 ,10 ,01 ,0 ,0 ,0 ,0 ,ZoneId.of("Asia/Shanghai" )); ZonedDateTime zonedDateTime = ZonedDateTime.of(2020 ,10 ,01 ,0 ,0 ,0 ,0 ,ZoneOffset.of("+8" )); zonedDateTime.withZoneSameLocal(ZoneId.of("Z" )); zonedDateTime.withZoneSameInstant(ZoneId.of("Z" ));
Period/Duration
1 2 3 4 5 6 7 8 9 10 11 LocalDate date = LocalDate.now(); LocalDate date1 = LocalDate.of(2000 ,11 ,1 ); Period period = Period.between(date, date1) System.out.println(period); LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime1 = LocalDateTime.of(2018 , 1 , 1 , 1 , 0 ); System.out.println(Duration.between(localDateTime, localDateTime1));