I have this input:
{
"MONDAY" : "02:39:00"
}
Which is a DAY and time that needs to compared to
"availability" : {
"SUNDAY" : {
"from" : "00:00",
"to" : "10:00"
},
"MONDAY" : {
"from" : "00:00",
"to" : "10:00"
}
}
What's the best way to compare if the availability
in fact contains a DAY in which the time in the query 02:39:00
is within.
As such, it can be represented in Java-form:
boolean isOpen = checkIfOpen("MONDAY", "02:39:00", availabilityJSONObject);
And which in this case, the value of isOpen
would be true.
As Pranav balu already said, use Java objects/data structures for your data. Use a JSON library like Jackson or Gson for converting your JSON input to Java types. You will need a Java type for the daily availability range. For example:
I have provided a convenience constructor and a convenience method that accept
String
arguments. You may want a constructor and a method that acceptLocalTime
, or both.Example use:
Output:
I am exploiting the fact that your time strings are in ISO 8601 format, and
LocalTime
parses this format as its default, that is, without any explicit formatter. The seconds are optional in the format, so both00:00
and02:39:00
are parsed.