新时间与日期API
作者:互联网
1. LocalDate LocatTime LocalDateTime
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime ldt = LocalDateTime.of(2020, 10, 20, 20, 0, 0);
ldt.plusYears(2);
ldt.minusMonths(2);
ldt.getYear();
ldt.getMonthValue();
ldt.getDayOfMonth();
ldt.getHour();
ldt.getMinute();
ldt.getSecond();
2. Instant:时间戳(Unix元年:1970.1.1 00:00:00)
// 默认获取UTC 时区
Instant ins= Instant.now();
// 带偏移量计算的
OffsetDateTime odt = ins.atOffset(ZoneOffset.ofHours(8));
3. Duration:计算两个“时间”之间的间隔 Period:计算两个“日期”之间的间隔
Duration duration = Duration.between(ins, ins2);
duation.toMillis();
LocalDate ld = LocalDate.of(2020, 1, 1);
LocalDate ld2 = LocalDate.now();
Period period = Period.between(ld, ld2);
period.getYears();
period.getMonths();
period.getDays();
4. TemporalAdjuster:时间校正器
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime ldt2 = ldt.with(TemproalAdjuster.next(DayOfWeek.SUNDAY));
.// 自定义:下一个工作日
ldt.with(date -> {
LocalDateTime ldt3 = (LocalDateTime)date;
DayOfWeek dow = ldt3.getDayOfWeek();
if(dow.equals(DayOfWeek.FRIDAY)){
return ldt3.plusDays(3);
} else if(dow.equals(DayOfWeek.SATURDAY)){
return ldt3.plusDays(2);
} else {
return ldt3.plusDays(1);
}
});
5. DateTimeFormatter:格式化时间/日期
6. ZonedDate ZonedTime ZonedDateTime:时区
标签:DayOfWeek,ldt,ldt3,period,日期,API,时间,LocalDateTime,LocalDate 来源: https://www.cnblogs.com/xzjl-23/p/14323393.html