I am looking to pass in a date String and check if this date is before the current MST time.
I want to be able to reliably test this locally and when I deploy it.
Locally I am based in the UK. And when I deploy it, server is in Phoenix.
The time now in UK is 2024-06-28 15.45.00
and this logic produces true for isValidDate at present when I pass in 2024-06-28 15.00.00
.
But I am setting the zone to MST. I was expecting this to be false.
MST time is like 8am now. So it’s not before. It seems to continue to work against UK time.
How can I update this so that when I deploy it, it will check the date string against MST time.
And locally continue to run for MST too? Essentially if I end up in another server in Australia, logic should continue to work against MST time.
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH.mm.ss");
private static final ZoneId MST_TIMEZONE = ZoneId.of("America/Phoenix");
// Spring bean set to Clock.systemDefaultZone(); Can't change this.
// using a clock for unit testing purposes.
private final Clock clock;
private ZonedDateTime parseDate(String dateStr) {
try {
return LocalDateTime
.parse(dateStr, DATE_FORMATTER)
.atZone(MST_TIMEZONE);
} catch (DateTimeParseException e) {
return null;
}
}
private boolean isValidDate(String startDateTime) {
ZonedDateTime start = parseDate(startDateTime);
return start != null
&& start.isBefore(LocalDateTime.now(clock).atZone(MST_TIMEZONE));
}