You say 'in format YYYY-MM-DD', which suggests what you actually get are String objects, not Date objects. Dates, LocalDates, Instants, etc, these don't have a format. They represent the thing they are named after (except Date, which is badly misnamed, and actually represents a timezoneless instant based on epochmillis and has absolutely no relation whatsoever to the human concept of dates).
To work with dates, you want to.. have date objects. Or rather, the right type from the java.time
package. If it's just a date, you're looking for LocalDate
. These are timezoneless. They just represent the concept of, say, 'april 4th, 1985', without location being involved. The inputs you have aren't dates. They are effectively moments in time, given that it's a minutiously specified time (with 6 digits for second fractions no less), and includes a timezone, which is a timezone no humans use for normal purposes even. That suggests what you're really looking for is Instant
.
You then want to do something really bizarre to the instant: Check the DATE. You're mixing the notion of a moment in time, with the notion of a date, which is fundamentally not a moment in time. After all, if it's the 4th of april in the netherlands, it could well be the 3rd of april in new york. If it's October 25th, 1917 in Moscow, it is November 7th in Amsterdam. Trying to equate 'dates' is nonsensical without first localizing. Who are you asking? Where did they live?
There are many ways you can turn the various types in the java.time
package from instants to dates and back. Here's one approach:
public static Instant read(String in) {
return Instant.parse(in);
}
public static LocalDate localize(Instant i, ZoneId zone) {
return i.atZone(zone).toLocalDate();
}
public static void main(String[] args) throws Exception {
Instant a = Instant.parse("2020-06-09T07:10:28.1985307Z");
Instant b = Instant.parse("2020-04-09T07:10:28.1985307Z");
Instant c = Instant.parse("2020-06-09T23:10:28.1985307Z");
ZoneId ams = ZoneId.of("Europe/Amsterdam");
LocalDate aDate = localize(a, ams);
LocalDate bDate = localize(b, ams);
LocalDate cDate = localize(c, ams);
System.out.println(aDate.isEqual(cDate));
}
The above would end up printing 'false'. The instant in time described by 2020-06-09T07:10:28.1985307Z would be described as being the 9th of june in 2020 by someone in amsterdam. The instant 2020-06-09T23:10:28.1985307Z would be described as the 10th (as amsterdam would at that point by +0200). Switch the zone to new york, and the answer would be true instead.
The question 'are these 2 instants the same date' is not a sane question to ask without also including in that question which zone is asking. Note that UTC is itself a zone, you can use it: ZoneId utc = ZoneOffset.UTC;
will get you there.