Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change timezone in ZonedDateTime for Java

What I mean is this. Suppose I have a date object that is May 1, 2018, 12:00AM. I would like to create a ZonedDateTime object that is May 1, 2018, 12:00AM but of ANOTHER time zone (say EST). Because if I pass in that date object into ZonedDateTime, it treats that date object as UTC (so it treats it as May 1 2018, 12:00AM GMT), and I want it to preserve that date field values but change the timzeone to EST (May 1st 2018, 12:00AM EST). Is there a way to do this in Java?

like image 380
Dan James Avatar asked May 02 '18 10:05

Dan James


People also ask

How do I change the timezone in Java?

You can make use of the following DateFormat. SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); myDate. setTimeZone(TimeZone. getTimeZone("UTC")); Date newDate = myDate.

Is ZonedDateTime a UTC?

A ZonedDateTime represents a date-time with a time offset and/or a time zone in the ISO-8601 calendar system. On its own, ZonedDateTime only supports specifying time offsets such as UTC or UTC+02:00 , plus the SYSTEM time zone ID.

How do I convert UTC to ZonedDateTime?

Step 1: use the Date. toInstant() method to convert the Date object to an Instant object. Step 2: use the Instant. atZone(ZoneId zone) method to convert the Instant object of step 1 to a ZonedDateTime object in UTC time zone.

How do I change the timezone in Java 8?

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time.


1 Answers

What you need is a LocalDate:

    LocalDate date = LocalDate.of(2018, Month.MAY, 1);

This will be understood in all time zones and will never be anything else than May 1. The “Local” in this and other class names in java.time means “without timezone”.

If you do insist on a ZonedDateTime, the answer is withZoneSameLocal:

    ZonedDateTime zdt = date.atStartOfDay(ZoneOffset.UTC);
    ZonedDateTime inEst = zdt.withZoneSameLocal(ZoneId.of("Australia/Brisbane"));
    System.out.println(inEst);

Output:

2018-05-01T00:00+10:00[Australia/Brisbane]

Don’t rely on EST or other three and four letter time zone abbreviations. EST, for example, is ambiguous (used both in North America and Australia) and is not a time zone (used less than half of the year). Instead give region/city, for example America/Atikokan.

If by “a date object” you meant an object of the outdated java.util.Date class (avoid them if you can, prefer the modern classes in java.time):

    Date oldfashionedDate = // …;

    OffsetDateTime dateTime = oldfashionedDate.toInstant().atOffset(ZoneOffset.UTC);
    if (! dateTime.toLocalTime().equals(LocalTime.MIDNIGHT)) {
        throw new IllegalStateException("java.util.Date was supposed to be at midnight in UTC but was " + dateTime);
    }
    LocalDate date = dateTime.toLocalDate();
    System.out.println(date);

Output:

2018-05-01

like image 126
Ole V.V. Avatar answered Sep 29 '22 08:09

Ole V.V.