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
LocalDatehas only a date (day, month and year), while theZonedDateTimealso 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
ZonedDateTimeto aLocalDate. Supposing we have:You can use the methods
equals,isBeforeorisAfterto check if the local date (day/month/year) is the same, before or after the otherYou can also use
compareTo, which can return-1,0or1if 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 theLocalDatehas only these fields, you don't need the others to do the comparison.To do the opposite (convert the
LocalDateto 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
equalschecks if all fields are the same (date, time and timezone), while all other methods compare the equivalentInstant(even if the timezones are different, the correspondingInstantis used to do the comparison).In this case,
z2is2017-08-23T00:00Z(August 23th 2017 at midnight in UTC), whilezis2017-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,
equalsreturnsfalsebecause the time and timezone are different andisEqualsreturnsfalsebecause they don't represent the same instant (even when converting to UTC, they're are different).isBeforereturnstruebecausez2is 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
ZoneIdclass:The API uses IANA timezones names (always in the format
Region/City, likeAmerica/Sao_PauloorEurope/Berlin). Avoid using the 3-letter abbreviations (likeCSTorPST) 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.