sophist_pt
sophist_pt

Reputation: 329

Comparing two different string formats for UTC that represents timestamp in java

I have a string "21-SEP-15 02.48.48.000000000 AM UTC" which represents utc timestamp and I have "2015-10-08T20:13:21.3Z" format string which represents another timestamp. How do I compare them if they are equal or not?

Upvotes: 0

Views: 1220

Answers (1)

assylias
assylias

Reputation: 328659

The second format uses the ISO convention so it can be parsed easily.

The first one is a bit more complicated to parse, in particular because the month is in upper case.

One way would be:

String ts1 = "21-SEP-15 02.48.48.000000000 AM UTC";
String ts2 = "2015-10-08T20:13:21.3Z";

DateTimeFormatter fmt1 = new DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a VV")
        .toFormatter(Locale.ENGLISH);

Instant d1 = ZonedDateTime.parse(ts1, fmt1).toInstant();
Instant d2 = Instant.parse(ts2);

You can then compare the two instants with d1.equals(d2).

Upvotes: 2

Related Questions