How would I calculate the time difference in minutes from the current time against a date/time in the format of "August 3, 2021 at 4:29 PM"?
Asked
Active
Viewed 33 times
0
-
2You would use suitable classes from [`java.time`](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) for that. – deHaar Aug 04 '21 at 08:12
-
3Parse the date using a `DateTimeFormatter`. Then get the current time `ZonedDateTime.now()` and do `ChronoUnit.MINUTES.between(first, second)`, done. – Zabuzard Aug 04 '21 at 08:12
-
1In which time zone is *August 3, 2021 at 4:29 PM*? – Ole V.V. Aug 04 '21 at 18:33
-
@OleV.V. U.S. Pacific Time – selbay84 Aug 04 '21 at 21:39
-
2So you want `ChronoUnit.MINUTES.between(ZonedDateTime.now(zone), LocalDateTime.parse("August 3, 2021 at 4:29 PM", DateTimeFormatter.ofPattern("MMMM d, uuuu 'at' h:mm a", Locale.US)).atZone(zone))`. Only split over multiple statements. With `zone` set to `ZoneId.of("America/Los_Angeles")` it just yielded `-1750`. A negative number since the time has past. – Ole V.V. Aug 05 '21 at 04:40