private static void test5(){ //当前日期 LocalDateTime ldt=LocalDateTime.now(); System.out.println(ldt);//2017-11-04T13:46:39.537 //把日期调整为这月的10号,使用with来设置日 LocalDateTime ldt2=ldt.withDayOfMonth(10);//2017-11-04T13:46:39.537 System.out.println(ldt2); //下一个周日,使用with来设置日 LocalDateTime ldt3=ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); System.out.println(ldt3);//2017-11-05T13:48:37.038 //下一个周一,使用with来设置日 LocalDateTime ldt4=ldt.with(TemporalAdjusters.next(DayOfWeek.MONDAY)); System.out.println(ldt4);//2017-11-06T13:51:24.466 //自定义下一个工作日 参数是函数式接口TemporalAdjuster Temporal adjustInto(Temporal temporal); LocalDateTime ldt5=ldt.with(t->{ LocalDateTime ldtInput=(LocalDateTime)t; DayOfWeek dayOfWeek=ldtInput.getDayOfWeek(); if(dayOfWeek.equals(DayOfWeek.MONDAY)){ return ldtInput.plusDays(7); } else if(dayOfWeek.equals(DayOfWeek.THURSDAY)){ return ldtInput.plusDays(6); } else if(dayOfWeek.equals(DayOfWeek.WEDNESDAY)){ return ldtInput.plusDays(5); } else if(dayOfWeek.equals(DayOfWeek.THURSDAY)){ return ldtInput.plusDays(4); } else if(dayOfWeek.equals(DayOfWeek.FRIDAY)){ return ldtInput.plusDays(3); } else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){ return ldtInput.plusDays(2); } else { return ldtInput.plusDays(1); } }); System.out.println(ldt5);//2017-11-06T13:59:51.672 }
时间矫正器1
时间: 2024-10-09 01:44:59