2

I have two dates in String format as: say String date1 = 2018-08-29 and in ISO-OFFSET_DATE_TIME format as String date2 = 2018-08-30T00:00:00+10:00. What is the best way to compare date1 and date2 for equality? I am not concerned about time, only day, year and month matters. Should I be converting them to instant and compare?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Krishna
  • 137
  • 1
  • 2
  • 8
  • `LocalDate.parse(date1).equals(OffsetDateTime.parse(date2).toLocalDate())` – shmosel Aug 30 '18 at 00:31
  • That would be the easiest way. You may need to account for any formats that vary from these two if you are unsure of the input. If date1 and date2 are always in the described format, you should be good to go. If you would like me to write it up for you, I would be happy to. – SynchroDynamic Aug 30 '18 at 00:33
  • `date1` is `LocalDate`, `date2` is `OFFSET_DATE_TIME`, may I ask what is the expected out put? – xingbin Aug 30 '18 at 03:48
  • @Sun, isn’t that explained well in the question? I find it perfectly clear. We should only compare the calendar dates and ignore the time of day. The question is whether the dates are equal or not. – Ole V.V. Aug 30 '18 at 05:34
  • 1
    @OleV.V. I'm not sure whether the OP need change the `OFFSET` before comparing. – xingbin Aug 30 '18 at 10:23
  • 2
    That’s a good and interesting question, @Sun, since it is never the same date in all time zones. – Ole V.V. Aug 30 '18 at 10:51

3 Answers3

3

the fastest way is;

String date1 = "2018-08-29";
String date2 = "2018-08-30T00:00:00+10:00";
boolean isEqual = date2.startsWith(date1);
JunJie Wang
  • 460
  • 3
  • 10
3

tl;dr

LocalDate
.parse( "2018-08-29" ) 
.isEqual(
    OffsetDateTime
    .parse( "2018-08-30T00:00:00+10:00" )
    .toLocalDate()
)

false

java.time

Parse each input during its appropriate type in java.time.

LocalDate ld = LocalDate.parse( "2018-08-29" ) ;
OffsetDateTime odt = OffsetDateTime.parse( "2018-08-30T00:00:00+10:00" ) ;

Compare by extracting a LocalDate from the OffsetDateTime.

Boolean sameDate = ld.isEqual( odt.toLocalDate() ) ;

false

Or perhaps you want to adjust that OffsetDateTime from its offset of ten hours ahead of UTC to another offset or time zone. For example, let’s adjust back to UTC before extracting a date to compare.

LocalDate
.parse( "2018-08-29" ) 
.isEqual(
    OffsetDateTime
    .parse( "2018-08-30T00:00:00+10:00" )
    .withOffsetSameInstant( ZoneOffset.UTC )
    .toLocalDate()
)

This changes our results from false, seen in code above, to true.

true

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If this is the only thing you will ever want to know about the two strings:

    LocalDate ld1 = LocalDate.parse(date1);
    LocalDate ld2 = LocalDate.parse(date2, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    if (ld1.isEqual(ld2)) {
        System.out.println("They are the same date");
    } else {
        System.out.println("They are different dates");
    }

Output using your string examples frmo the question:

They are different dates

I was just writing code similar to that by Basil Bourque when his answer came. I prefer to handle date and time by the correct date-time objects, which is exactly what he’s doing. By parsing date2 into an OffsetDateTime you are keeping all information, and the conversion to LocalDate for comparison is easy. For most purposes I recommend that way.

Only if you know for sure that you will never need the time of day, you may take a shortcut. You may of course use the fast way by JunJie Wang. I prefer my own code above, though, for two reasons: I tells the reader clearly that the strings are dates and times and handles them correctly as such. And it thereby also provides validation of the string formats. If some day by accident you get a completely different string, you will be notified through a DateTimeParseException.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161