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.
How to convert ZonedDateTime to Date in Java
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 the 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 straightforward 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.
Conclusion
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 :)
Frequently Asked Questions(FAQs)
1. Can I convert a ZonedDateTime object to a Date object directly in Java?
No, the ZonedDateTime class does not provide a direct conversion method to the Date class. However, you can utilize the toInstant() method and convert the Instant to a Date object using java.util.Date.from().
2. What is the difference between ZonedDateTime and Date in Java?
ZonedDateTime represents a date, time, and time zone, providing a more comprehensive and flexible approach to handle dates. Date, on the other hand, is a legacy class that represents a specific moment in time but lacks the functionality to handle time zones and daylight saving time.
3. How can I convert a ZonedDateTime to a Date without losing time zone information?
To convert a ZonedDateTime to a Date while preserving time zone information, you can use the toInstant() method to obtain an Instant and then convert it to a Date using java.util.Date.from().
4. Can I convert a ZonedDateTime to a Date and back to ZonedDateTime in Java?
Yes, it is possible to convert a ZonedDateTime to a Date and then convert the Date back to a ZonedDateTime using the toInstant() method and the java.sql.Timestamp class.
5. Are there any third-party libraries in Java that simplify ZonedDateTime to Date conversions?
Yes, several third-party libraries, such as Joda-Time and the ThreeTen Extra library, provide additional utilities and methods to facilitate date and time conversions, including conversions between ZonedDateTime and Date objects.
You may also like: