// v 2024-02-02 07:52:57 2024-02-02 14:39:14
//Date inTime = Date.from(Instant.ofEpochMilli(1707150614000L));
//LocalDateTime inTimeLocalDateTime = DateUtil.dateToLocalDateTime(inTime);
LocalDateTime inTime = LocalDateTime.parse("2024-02-01 16:52:57", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//LocalTime intimelocalTime = inTime.toLocalTime();
//
//String intimelocalDate = LocalDate.now().toString();
//
//LocalTime startTime = LocalTime.of(8, 0, 0,0);
//LocalTime endTime =LocalTime.of(20, 0, 0,0);
//if (intimelocalTime.compareTo(startTime)>=0 && intimelocalTime.compareTo(endTime)<1){
// //在时段1 范围内
// //2024-01-19 16:25:01 2024-01-19 20:00:00
// System.out.println(intimelocalDate+intimelocalTime+"时段:"+1);
//}
//else {
// System.out.println(intimelocalDate+intimelocalTime+"时段:"+2);
//}
//LocalDateTime zeroTime = intimelocalDate.atTime(0, 0, 0, 0);
//if (intimelocalTime.compareTo(zeroTime)>=0 && intimelocalTime.compareTo(oneDateStartTime)<0){
// System.out.println(inTime+" "+oneDateStartTime+" "+inTime.toLocalDate()+"时段:"+2);
//}
//LocalDateTime outTimeLocalDateTime = LocalDateTime.parse("2024-02-02 14:39:14",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//2024-02-02 07:52:5 2024-02-02 08:00:00
//2024-02-02 08:00:00 2024-02-02 14:39:00
//2024-01-20 08:00:00 2024-01-20 20:00:00
//2024-01-20 20:00:00 2024-01-21 08:00:00
//2024-01-21 08:00:00 2024-01-21 10:13:09
//08:00:00 20:00:00
//Period.between()
//LocalDateTime inTime=inTimeLocalDateTime;
//LocalDateTime outTime=outTimeLocalDateTime;
LocalDate intimelocalDate = inTime.toLocalDate();
LocalDateTime oneDateStartTime = intimelocalDate.atTime(8, 0, 0,0);
LocalDateTime oneDateEndTime =intimelocalDate.atTime(20, 0, 0,0);
//LocalDateTime oneDateEndTime = oneDateEndTimeinit.compareTo(outTimeLocalDateTime) > 0 ? outTimeLocalDateTime : oneDateEndTimeinit;
if (inTime.compareTo(oneDateStartTime)>=0 && inTime.compareTo(oneDateEndTime)<1){
//在时段1 范围内
//2024-01-19 16:25:01 2024-01-19 20:00:00
System.out.println(inTime+" "+oneDateEndTime+" "+inTime.toLocalDate()+"时段:"+1);
}
LocalDateTime twoDateStartTime = intimelocalDate.atTime(20, 0, 0,0);
LocalDateTime twoDateEndTime =intimelocalDate.plusDays(1).atTime(8, 0, 0,0);
if (inTime.compareTo(twoDateStartTime)>=0 && inTime.compareTo(twoDateEndTime)<1){
System.out.println(inTime+" "+twoDateEndTime+" "+inTime.toLocalDate()+"时段:"+2);
}
LocalDateTime zeroTime = intimelocalDate.atTime(0, 0, 0, 0);
if (inTime.compareTo(zeroTime)>=0 && inTime.compareTo(oneDateStartTime)<0){
System.out.println(inTime+" "+oneDateStartTime+" "+inTime.toLocalDate()+"时段:"+2);
}
//while (inTime.compareTo(outTimeLocalDateTime)<0){
// LocalDate intimelocalDate = inTime.toLocalDate();
// LocalDateTime oneDateStartTime = intimelocalDate.atTime(8, 0, 0,0);
// LocalDateTime oneDateEndTimeinit =intimelocalDate.atTime(20, 0, 0,0);
// LocalDateTime oneDateEndTime = oneDateEndTimeinit.compareTo(outTimeLocalDateTime) > 0 ? outTimeLocalDateTime : oneDateEndTimeinit;
// if (inTime.compareTo(oneDateStartTime)>=0 && inTime.compareTo(oneDateEndTime)<1){
// //在时段1 范围内
// //2024-01-19 16:25:01 2024-01-19 20:00:00
// System.out.println(inTime+" "+oneDateEndTime+" "+inTime.toLocalDate()+"时段:"+1);
// inTime=oneDateEndTime;
// }
// LocalDateTime twoDateStartTime = intimelocalDate.atTime(20, 0, 0,0);
// LocalDateTime twoDateEndTimeinit =intimelocalDate.plusDays(1).atTime(8, 0, 0,0);
// LocalDateTime twoDateEndTime = twoDateEndTimeinit.compareTo(outTimeLocalDateTime) > 0 ? outTimeLocalDateTime : twoDateEndTimeinit;
// if (inTime.compareTo(twoDateStartTime)>=0 && inTime.compareTo(twoDateEndTime)<1){
// System.out.println(inTime+" "+twoDateEndTime+" "+inTime.toLocalDate()+"时段:"+2);
// }
// LocalDateTime zeroTime = intimelocalDate.atTime(0, 0, 0, 0);
// if (inTime.compareTo(zeroTime)>=0 && inTime.compareTo(oneDateStartTime)<0){
// System.out.println(inTime+" "+oneDateStartTime+" "+inTime.toLocalDate()+"时段:"+2);
// }
// inTime = twoDateEndTime;
//
//}
//LocalDate intimelocalDate = inTimeLocalDateTime.toLocalDate();
//long between = ChronoUnit.DAYS.between(inTimeLocalDateTime.toLocalDate(), outTimeLocalDateTime.toLocalDate());
//for (long i = 0; i < between; i++) {
// LocalDate intimelocalDateNew = intimelocalDate.plusDays(i);
// LocalDateTime oneDateStartTime = intimelocalDateNew.atTime(8, 0, 0);
// LocalDateTime oneDateEndTime =intimelocalDateNew.atTime(20, 0, 0);
// LocalDateTime inTimeLocalDateTimeNew=inTimeLocalDateTime;
// if (inTimeLocalDateTimeNew.isAfter(oneDateStartTime) && inTimeLocalDateTimeNew.isBefore(oneDateEndTime)){
// //在时段1 范围内
// //2024-01-19 16:25:01 2024-01-19 20:00:00
// String string = inTimeLocalDateTimeNew.toString();
// System.out.println(string+" "+oneDateEndTime);
//
//
// }
// // 2 20:00:00 08:00:00
// System.out.println("22222222222");
//
//
//
//}
public static LocalDateTime getAppointLocalDateTime2(LocalDateTime localDateTime,int hour,int minute,int second){
// 设置时间为
return localDateTime.withHour(hour).withMinute(minute).withSecond(second).withNano(0);
}
public static LocalDateTime getAppointLocalDateTime(int hour,int minute,int second){
// 获取当前日期和时间
LocalDateTime now =LocalDateTimeNow();
// 设置时间为
return now.withHour(hour).withMinute(minute).withSecond(second).withNano(0);
}
public static LocalDateTime getNextLocalDateTime(int hour,int minute,int second){
// 获取当前日期和时间
LocalDateTime now =LocalDateTimeNow().plusDays(1);
// 设置时间为
return now.withHour(hour).withMinute(minute).withSecond(second).withNano(0);
}
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
}
public static LocalDateTime dateToLocalDateTime(Date nowDate) {
return nowDate.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
}
public static Date LocalDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
}
public static Date LocalDateToDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
}
public static LocalDateTime LocalDateTimeNow() {
return LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
}
public static LocalDate LocalDateNow() {
return LocalDate.now(ZoneId.of("Asia/Shanghai"));
}
public static Long dateTimeStrToInstant(String datetime){
LocalDateTime localDateTime = LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
}
标签:return,归属,LocalDateTime,时间段,compareTo,static,inTime,public
From: https://blog.51cto.com/u_16253752/9639137