CLOSE

   Java  Date  ZonedDateTime  
   Technology    Programming

How to convert ZonedDateTime to Date in Java?

         
 JANUARY 4, 2020   by rohit0903

In this blog, we will see what ZonedDateTime is and why Oracle introduced this class in Java. Our major focus will be on the conversion mechanism from ZonedDateTime to a Date instance of Java. To understand the conversion process properly we should first see what actually ZonedDateTime, LocalDateTime and Instant is and what is the major difference between them.

Let's see what these 3 classes are and how differently they behave in different time zones.


ZonedDateTime, LocalDateTime and Instant class

The java.time.ZonedDateTime class in the Java 8 date time API is an immutable representation of a date-time which represents a date and time along with timezone information. As it is linked with zone, we can specify any time information along with the zone of any region across the globe, for example, a conference meeting that's going to be in Munich or a Football match that going to take place in Berlin, etc.

Now, let us focus on the LocalDateTime, ZonedDateTime and Instant class, all three of these appear to be quite similar, so it's worth understanding how each differs.

package com.studyTonight;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;

public class ZonedDateAndLocalDate {

    public static void main(String args[]) {
        // Initialize Instant object
        Instant instNow = Instant.now();

        // Initialize LocalDateTime object
        LocalDateTime ldtNow = LocalDateTime.now();
        
        // Initialize ZonedDateTime object
        ZonedDateTime zdtNow = ZonedDateTime.now();
        
        // Let's try to print
        System.out.println(instNow);
        System.out.println(ldtNow);
        System.out.println(zdtNow);
    }
}

Output:

2019-12-09T11:29:53.527Z
2019-12-09T11:29:53.559
2019-12-09T11:29:53.560Z[Asia/India]

Let's see the output one by one. The first, the Instant class instance tells us that this code was run at 11.29 am on 9th December 2019. The letter Z at the end stands for Zulu time, which is also known as GMT (Greenwich Mean Time) or UTC (which stands for Coordinated Universal Time).

The second result is the local date-time - that's a representation of my current time, which is 11.29 am on 9th December 2019, that's what my computer clock and calendar is showing. And the third item is the zoned date-time, where we can again see that it's 11.29 am on 9th December 2019, India time.

So the major learning from the above example is that the ZonedDateTime is showing LocalDateTime along with zone information. In short, we can say it is a combination of Instant and Zone info.


Now, I think the conversion will be pretty much straight forward and simple after seeing below piece of code:

package com.studyTonight;

import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * The class shows how to convert ZonedDateTime to Date instance.
 * @author StudyTonight
 *
 */
public class ZonedDateTimeToDate {

    public static void main(String args[]) {
        // Create ZonedDateTime object
        ZonedDateTime zdt = ZonedDateTime.now();
        
        // Convert ZonedDateTime to Instant instance
        // We just say what Instant is and how it used in the blog
        Instant instant = zdt.toInstant();
        
        // Create Date instance out of Instant
        Date date = Date.from(instant);
        
        // Print Date value
        System.out.println("Value : " + date);
    }
    
}

Output:

Value : Mon Dec 09 11:34:15 IST 2019

As shown in the above example, ZonedDateTime gets converted to Instant which further gets converted to a Date object.

I think you all are now good with what ZonedDateTime is and how this conversion works. The main outcome after reading this blog should be the major difference between ZonedDateTime, Instant and LocalDateTime along with the conversion process.

I hope you all have sound knowledge of this Java API now. In case of any issues please feel free to get in touch with us. Keep learning :)


SHARE YOUR THOUGHTS WITH US!