linux元年时间搓,新时间与日期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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值