import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
String time = "2023-01-01 11:20:45";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime givenTime = LocalDateTime.parse(time, dtf);
LocalDateTime currentTime = LocalDateTime.now();
System.out.println("给定时间: " + givenTime);
System.out.println("当前时间: " + currentTime);
LocalDateTime plussed = givenTime.plusDays(7);
System.out.println("加7天后的时间: " + plussed);
if(currentTime.isAfter(plussed)) {
LocalDateTime newTime = generateNewTime(givenTime);
System.out.println("已经超过7天重新生成第六天的随机时间: " + newTime);
} else {
System.out.println("时间没有超过七天沿用currentTime");
}
}
private static LocalDateTime generateNewTime(LocalDateTime givenTime) {
Random random = new Random();
LocalDateTime newTime = givenTime.plusDays(6)
.withHour(random.nextInt(24))
.plusMinutes(random.nextInt(60))
.plusSeconds(random.nextInt(60));
if (newTime.equals(givenTime)) {
return generateNewTime(givenTime);
}
return newTime;
}
}