I'm looking for a way to compare instances of LocalDate
and ZonedDateTime
in js-joda
, but those classes are not compatible. I'm willing to accept, that LocalDate
is a ZonedDateTime
with time and timezone set to zero.
What would be the easiest way to compare those two objects?
A
LocalDate
has only a date (day, month and year), while theZonedDateTime
also has a time (hour, minute, second and nanosecond) and a timezone. To compare them, you must choose which one will be converted to the other's type.As @Dummy's answer already explains, one way is to convert the
ZonedDateTime
to aLocalDate
. Supposing we have:You can use the methods
equals
,isBefore
orisAfter
to check if the local date (day/month/year) is the same, before or after the otherYou can also use
compareTo
, which can return-1
,0
or1
if the first date is before, equals or after the second one.As told in the comments, calling
toLocalDate()
discards the time and timezone information, keeping just the date (day/month/year). But as theLocalDate
has only these fields, you don't need the others to do the comparison.To do the opposite (convert the
LocalDate
to aZonedDateTime
), you'll have to make some assumptions. The same date can represent a different instant depending on the timezone you are (right now is August 23th in São Paulo, but in Tokyo is August 24th - the timezone you choose might affect the final result).And you'll also need to define at what time (hour/minute/second/nanosecond) this date will be. You told you can assume that LocalDate is a ZonedDateTime with time and timezone set to zero. This means that the time will be midnight (00:00) and the timezone will be UTC (offset zero):
Then you can compare:
Note that
equals
checks if all fields are the same (date, time and timezone), while all other methods compare the equivalentInstant
(even if the timezones are different, the correspondingInstant
is used to do the comparison).In this case,
z2
is2017-08-23T00:00Z
(August 23th 2017 at midnight in UTC), whilez
is2017-08-23T13:12:24.856-03:00[America/Sao_Paulo]
, which is equivalent in UTC to2017-08-23T16:12:24.856Z
(August 23th 2017 at 16:12:24.856 in UTC).So,
equals
returnsfalse
because the time and timezone are different andisEquals
returnsfalse
because they don't represent the same instant (even when converting to UTC, they're are different).isBefore
returnstrue
becausez2
is actually beforez
(midnight < 16:12) and so on.If you want to use another timezone instead of UTC, you must use js-joda-timezone and the
ZoneId
class:The API uses IANA timezones names (always in the format
Region/City
, likeAmerica/Sao_Paulo
orEurope/Berlin
). Avoid using the 3-letter abbreviations (likeCST
orPST
) because they are ambiguous and not standard.You can get a list of available timezones (and choose the one that fits best your system) by calling
ZoneId.getAvailableZoneIds()
.You can also use the system's default timezone with
ZoneId.systemDefault()
, but this will be system/browser dependent, so it's better to explicity use a specific one.