java8新特性_19_新时期与日期API_时间矫正器
程序员文章站
2022-05-05 08:54:51
...
//TemporalAdjuster 时间矫正器
@Test
public void test1(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
//日期变为10号
LocalDateTime ldt2 = ldt.withDayOfMonth(10);
System.out.println(ldt2);
//下一个星期日
LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(ldt3);
//lambda表达式,自定义转换器
LocalDateTime ldt4 = ldt.with(l->{
LocalDateTime ldt5 = (LocalDateTime) l;
DayOfWeek dow = ldt5.getDayOfWeek();
if(dow.equals(DayOfWeek.FRIDAY)){
return ldt5.plusDays(3);
}else if(dow.equals(DayOfWeek.SATURDAY)){
return ldt5.plusDays(2);
}else{
return ldt5.plusDays(1);
}
});
System.out.println(ldt4);
}