Convert Date and Time from One Time Zone to Another using Java

ZonedDateTime class

package app;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main
{
    public static void main(String[] args)
    {
        String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        ZoneId fromTz = ZoneId.of("Europe/London");
        ZoneId toTz = ZoneId.of("America/New_York");

        String fromDateTime = "2020-09-12 20:30:00";

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
        ZonedDateTime dt = LocalDateTime.parse(fromDateTime, formatter).atZone(fromTz);
        dt = dt.withZoneSameInstant(toTz);

        String toDateTime = formatter.format(dt);

        System.out.println(toDateTime);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.