LocalDate LocalTime LocalDateTime
日期时间
// 创建
LocalDateTime ldt1 = LocalDateTime.now();
LocalDateTime ldt2 = LocalDateTime.of(1997, 3, 26, 11, 11, 11);
LocalDateTime ldt3 = LocalDateTime.now(ZoneId.of("Europe/Tallinn")); // 指定时区
System.out.println(ldt1);
System.out.println(ldt2);
System.out.println(ldt3);
// 运算 plusXXX minusXXX getXXX
LocalDateTime ldt4 = ldt1.plusYears(2);
LocalDateTime ldt5 = ldt1.minusMonths(2);
System.out.println(ldt5); // 2020-12-02T09:40:01.537
System.out.println(ldt5); // 2018-10-02T09:40:01.537
System.out.println(ldt5.getMonth()); // DECEMBER
System.out.println(ldt5.getMonthValue()); // 12
System.out.println(ldt5.getDayOfWeek()); // WEDNESDAY
System.out.println(ldt5.getDayOfMonth()); // 2
System.out.println(ldt5.getDayOfYear()); // 337
Instant
时间戳
// 创建
Instant ins1 = Instant.now(); // 默认获取UTC时区
System.out.println(ins1); // 2018-12-02T01:46:57.064Z
Instant ins2 = Instant.ofEpochSecond(60);
System.out.println(ins2); // 1970-01-01T00:01:00Z
// 改时区
OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8));
System.out.println(odt); // 2018-12-02T09:46:57.064+08:00
// 时间戳
System.out.println(ins1.toEpochMilli()); // 1543715337658
Duration Period
Duration:计算两个时间之间的间隔。
Instant ins1 = Instant.now();
Thread.sleep(1000);
Instant ins2 = Instant.now();
Duration duration = Duration.between(ins1, ins2);
System.out.println(duration.toMillis());
// --------------------------------------------
LocalTime ldt1 = LocalTime.now();
Thread.sleep(1000);
LocalTime ldt2 = LocalTime.now();
System.out.println(Duration.between(ldt1, ldt2).toMillis());
Period:计算两个日期之间的间隔。
LocalDate ld1 = LocalDate.of(2015, 1, 1);
LocalDate ld2 = LocalDate.now();
Period period = Period.between(ld1, ld2);
System.out.println(period); // P3Y11M1D
System.out.println(period.getYears()); // 3
TemporalAdjuster
时间校正器
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt); // 2018-12-02T10:08:57.684
// 当月的第十天
// 这样的写法语义有限,使用TemporalAdjuster
LocalDateTime ld2 = ldt.withDayOfMonth(10);
System.out.println(ld2); // 2018-12-10T10:08:57.684
// 下一个周日
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3); // 2018-12-09T10:12:25.633
// 下一个工作日
LocalDateTime ldt5 = ldt.with((l) -> {
LocalDateTime ldt4 = (LocalDateTime) l;
DayOfWeek dow = ldt4.getDayOfWeek();
if (dow.equals(DayOfWeek.FRIDAY)) {
return ldt4.plusDays(3);
} else if (dow.equals(DayOfWeek.SATURDAY)) {
return ldt4.plusDays(2);
} else {
return ldt4.plusDays(1);
}
});
System.out.println(ldt5); // 2018-12-03T10:21:04.932
DateTimeFormatter
格式化
// 使用 LocalDateTime.format方法
LocalDateTime ldt = LocalDateTime.now();
// 2018-12-02T10:24:08.098
System.out.println(ldt.format(DateTimeFormatter.ISO_DATE_TIME));
// 2018-12-02
System.out.println(ldt.format(DateTimeFormatter.ISO_DATE));
// 2018年12月02日 10:26:02
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss")));
// 使用 DateTimeFormatter.format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
System.out.println(dtf.format(LocalDateTime.now()));
// parse
LocalDateTime newDate = ldt.parse("2018年12月02日 10:32:55", dtf);
System.out.println(newDate);
ZonedDate、ZonedTime、ZonedDateTime
Set<String> set = ZoneId.getAvailableZoneIds();
set.forEach(System.out::println);
// -----------------------------------
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ldt.atZone(ZoneId.of("Europe/Tallinn"));
System.out.println(zdt); // 2018-12-02T10:44:22.142+02:00[Europe/Tallinn]
LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
ZonedDateTime zdt2 = ldt2.atZone(ZoneId.of("Europe/Tallinn"));
System.out.println(zdt2); // 2018-12-02T04:44:22.146+02:00[Europe/Tallinn]