0

I am reading a timestamp from a database as a string value, say:

2020-04-30T09:59:00.272-05:00

. Now i have to compare this with current timestamp in local timezone(System timezone)

What i have tried so Far:

ZonedDateTime result = ZonedDateTime.parse("2020-04-30T09:59:00.272-05:00"); System.out.println("Given : "+result); System.out.println("Given In Local: "+result.withZoneSameLocal(ZoneId.systemDefault()));

OutPut: Given : 2020-04-30T09:59:00.272-05:00 Given In Local: 2020-04-30T09:59:00.272-05:00[America/Chicago]

My Desired output is "2020-04-30T14:59:00.272"

user2048204
  • 729
  • 4
  • 13
  • 27
  • In your desired output you have thrown away the offset information ( `-05:00`). For a correct comparison don’t do that. Do you want to convert to UTC? Why? Asking because I see no reason to, you can compare `OffsetDateTime` and `ZonedDateTime` object across different time zones or offsets. If you do need to, us `ZoneOffset.UTC`. And mentioning `OffsetDateTime` because it seems a better fit for your database value than `ZonedDateTime`. – Ole V.V. Apr 26 '20 at 19:44

2 Answers2

1

You could parse the given date string as an OffsetDateTime, then change its offset and convert it to LocalDateTime as follows

    final String dateString = "2020-04-30T09:59:00.272-05:00";
    final ZonedDateTime result = ZonedDateTime.parse(dateString);
    System.out.println("Given         : " + result);
    final LocalDateTime localDateTime = OffsetDateTime.parse(dateString)
        .atZoneSameInstant(ZoneId.of("Europe/Berlin"))
        .toLocalDateTime();
    System.out.println("Given In Local: " + localDateTime);```

prints

Given         : 2020-04-30T09:59:00.272-05:00
Given In Local: 2020-04-30T16:59:00.272

Besides, you could also parse it to ZonedDateTime and then change the time zone, e.g.

final LocalDateTime result = ZonedDateTime.parse(dateString)
        .withZoneSameInstant(ZoneId.of("Europe/Berlin"))
        .toLocalDateTime();
Michael Kreutz
  • 1,216
  • 6
  • 9
1

I believe that the following should fulfil your purpose.

    String dateTimeString = "2020-04-30T09:59:00.272-05:00";
    OffsetDateTime databaseDateTime = OffsetDateTime.parse(dateTimeString );
    OffsetDateTime currentDateTime = OffsetDateTime.now(ZoneId.systemDefault());
    if (databaseDateTime.isAfter(currentDateTime)) {
        System.out.println("" + databaseDateTime + " is after " + currentDateTime);
    } else {
        System.out.println("" + databaseDateTime + " is before or equal to " + currentDateTime);
    }

Output when I ran the code just now in my time zone (Europe/Copenhagen):

2020-04-30T09:59:00.272-05:00 is after 2020-04-26T21:48:36.331752+02:00

Since your string from the database contains a UTC offset (-05:00) and no time zone (like America/New_York), I found it more appropriate to parse into an OffsetDateTime. ZonedDateTime would be overkill. For the local time I am passing ZoneId.systemDefault() to the now method of OffsetDateTime. Comparing the two OffsetDateTime objects with isAfter() works nicely even if the offsets are different, this is taken into account. There are also methods isBefore and isEqual for comparison.

Stepping a step back, in most setups you should not be getting the date and time as a string from your database. Your JDBC 4.2 driver or your modern JPA implementation such as Hibernate 5 will be happy to fetch an OffsetDateTime object from the database for you. See for example my answer here.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks @Ole, I think there is some problem with the way we storing the dateTIme in the db. But both the answers should solve my problem. – user2048204 Apr 27 '20 at 09:57