1

我正在尝试将当前系统的时间和日期与输入字符串时间和日期进行比较。我LocalDateTime.now()用来获取系统当前时间,也DateTimeFormatter用来将其格式化为 dd-MM-yyyy HH:mm 模式

static DateTimeFormatter frmt = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
static LocalDateTime time = LocalDateTime.now();

这是我所拥有的:

System.out.print("Enter the Appointment new Date and Time as dd-mm-yyyy hh:mm : ");
String dateAndTime = input.nextLine();

if (//dateAndTime is before or equals the current system time)
{
    System.out.println("The new date and time should be after the current time");
}
else
{
    System.out.println("Appointment has been updated");
}
4

1 回答 1

2

假设您已经解析了输入,LocalDateTime您可以执行以下操作:

if (parsedDateTime.isAfter(LocalDateTime.now())) {
    System.out.println("Appointment has been updated");
} else {
    System.out.println("The new date and time should be after the current time");
}

请记住,如果和完全相同,isAfter则返回 false 。parsedDateTimeLocalDateTime.now()

于 2021-12-02T16:59:30.290 回答