shahaf :
I have the following String representation of 24hr like 1423
and 1956
I would like to check if the current time (with time zone) is between this pair
my current solution gets the hour and minute of the day using
int h = Instant.now().atZone(ZoneId.systemDefault()).getHour()
int t = Instant.now().atZone(ZoneId.systemDefault()).getMinute()
creating a 4 digit strings from the numbers, and do a string comparison
I don't like my solution at all, What's the elegant way to do it?
Eng.Fouad :
You can convert String representation of time to LocalTime
as follows:
var formatter = DateTimeFormatter.ofPattern("HHmm");
var lowerTime = LocalTime.parse("1423", formatter);
var upperTime = LocalTime.parse("1956", formatter);
Then you can use isAfter()
/isBefore
methods to check if it is in between:
var nowTime = LocalTime.now(ZoneId.systemDefault());
boolean inBetween = nowTime.isAfter(lowerTime) && nowTime.isBefore(upperTime);