Convert datetime string in one timezone to another using offset

I have a datetime string "2018-01-15 01:16:00" which is in EST timezone. I want to convert this into another timezone dynamically using the UTC offset. My javascript code passes this UTC offset as a parameter and the servlet has to convert/format this datetime string to the timezone identified by the provided offset.

I have tried many approaches including the one documented in the oracle tutorials but unable to arrive at a solution.

Below is my code that I am trying, any help is greatly appreciated.

private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DEFAULT_TIME_ZONE = ZoneId.SHORT_IDS.get("EST");

public static void main(String[] args) throws Exception {
    String dateTime = "2018-01-15 02:35:00";
    //parse the datetime using LocalDateTime
    LocalDateTime defaultDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATE_FORMAT));

    //get the datetime in default timezone
    ZoneId defaultZoneId = ZoneId.of(DEFAULT_TIME_ZONE);
    ZonedDateTime defaultZoneDateTime = defaultDateTime.atZone(defaultZoneId);
    System.out.println("EST time: "+defaultZoneDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
    ZonedDateTime utcZonedDateTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
    String utcTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.println("UTC : "+utcTime);

    //IST timezone
    ZoneOffset offset = ZoneOffset.of("+05:30");
    OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);
    String targetTimeZone = offsetDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    System.out.printf("target time : "+targetTimeZone);



EST time: 2018-01-15 02:35:00
UTC : 2018-01-15 07:37:00
target time : 2018-01-15 07:37:00

Expected target time : 2018-01-15 13:05:00

Jon Skeet

The immediate problem is this line:

OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);

That's saying you want the same local date/time, but with the specified offset. That changes which instant in time is being represented.

Instead, you really want to represent the same instant in time, but at a particular offset. So the shortest fix is:

OffsetDateTime offsetDate = utcZonedDateTime.toInstant().atOffset(offset);

However, there are a number of other aspects which could do with changing:

  • Prefer ZoneOffset.UTC to ZoneId.of("UTC")
  • Using EST as a time zone is confusing - it's not clear whether you expect it to mean "Eastern Time" (changing between EST and EDT) or pure standard time of UTC-5. Assuming you actually mean "Eastern Time" it would be better to use America/New_York as a zone ID.
  • It's unclear what you want to happen if the input string represents a skipped or ambiguous value in Eastern time. These happen around DST transitions.

Next, you don't need to convert the ZonedDateTime in Eastern time into a ZonedDateTime in UTC at all. Either convert it directly to an instant:

OffsetDateTime target = defaultZoneDateTime.toInstant().at(offset);

Or create a ZonedDateTime for the target instead:

ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);

Given that an offset isn't really a time zone, I'd probably go with the first of these.


See more on this question at Stackoverflow