user2048204
user2048204

Reputation: 759

Java - Convert a given DateTime in String to local timezone

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"

Upvotes: 1

Views: 592

Answers (2)

Anonymous
Anonymous

Reputation: 86379

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.

Upvotes: 1

Michael Kreutz
Michael Kreutz

Reputation: 1256

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();

Upvotes: 2

Related Questions