Reputation: 45
Im trying to calculate two dates in a string if its less than 24 hours but I get an error:
java.time.format.DateTimeParseException: Text '20 4 01:40 AM' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor: {DayOfMonth=20,
MonthOfYear=4},ISO resolved to 01:40 of type java.time.format.Parsed
code:
String today = "20 4 7:00 AM";
String tomorrow = "21 4 8:00 AM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd M h:mm a");
LocalDateTime date1 = LocalDateTime.parse(today, formatter);
LocalDateTime date2 = LocalDateTime.parse(tomorrow, formatter);
long diffHours = Math.abs(Duration.between(date1, date2).toHours());
if (diffHours < 24) {
System.out.println("The difference is less than 24 hours.");
} else {
System.out.println("The difference is greater than or equal to 24 hours.");
}
Upvotes: 1
Views: 58