1

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"

1
  • 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. Commented Apr 26, 2020 at 19:44

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Michael, I tried your suggestion, but it is printing the same value i.e, "2020-04-30T09:59:00.272"
Can you try to replace ZoneId.systemDefault() with the ZoneId you want to be the time converted to?
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.