如何将系统当前的 LocalDateTime 与输入字符串日期和时间进行比较
How to compare system's current LocalDateTime with input String date and time
我正在尝试将当前系统的时间和日期与输入的字符串时间和日期进行比较。我正在使用 LocalDateTime.now()
获取系统当前时间,还使用 DateTimeFormatter
将其格式化为 dd-MM-yyyy HH:mm pattern
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");
}
假设您已经解析了 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");
}
请记住,如果 parsedDateTime
和 LocalDateTime.now()
完全相同,isAfter
将 return 为假。
我正在尝试将当前系统的时间和日期与输入的字符串时间和日期进行比较。我正在使用 LocalDateTime.now()
获取系统当前时间,还使用 DateTimeFormatter
将其格式化为 dd-MM-yyyy HH:mm pattern
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");
}
假设您已经解析了 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");
}
请记住,如果 parsedDateTime
和 LocalDateTime.now()
完全相同,isAfter
将 return 为假。