HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java ZonedDateTime class

Java ZonedDateTime class

java.time.ZonedDateTime class, introduced in Java 8, represents a date and time with timezone information in ISO-8601 calendar system. This class stores all date and time fields, to a precision of nanoseconds.

We can use the ZonedDateTime instances where we need to represent the time for globally distributed users. For example, we can use ZonedDateTime to communicate a conference date where joiners will connect online based on their local date and time.

A ZonedDateTime holds state equivalent to three separate objects, a LocalDateTime, a ZoneId and the resolved ZoneOffset.

Note that ZonedDateTime instances are immutable and thread-safe.

1. ZonedDateTime class declaration

public final class ZonedDateTime
	extends Object
	implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable
{
	//class body
}

2. How to create ZonedDateTime

Generally, we will be creating ZonedDateTime instances in two conditions i.e. get current timestamp with zone information or create represent a timestamp in a specific timezone.

2.1. Get current ZonedDateTime

Use following factory method to get the current timestamp.

ZonedDateTime now = ZonedDateTime.now();

ZonedDateTime now = ZonedDateTime.now( ZoneId.of("GMT+05:30") ); //Time in IST

2.2. Create specified ZonedDateTime

To create a timestamp with specific date, time and zone information – use below methods.

//1 - All date parts are inplace
ZoneId zoneId = ZoneId.of("UTC+1");

ZonedDateTime zonedDateTime1 =
    ZonedDateTime.of(2015, 11, 30, 23, 45, 59, 1234, zoneId);

//=========

//2 - Using existing local date and time values 
LocalDate localDate = LocalDate.of(2019, 03, 12);
LocalTime localTime = LocalTime.of(12,  44);
ZoneId zoneId = ZoneId.of("GMT+05:30");

ZonedDateTime timeStamp = ZonedDateTime.of( localDate, localTime, zoneId );

3. How to parse string to ZonedDateTime

The ZonedDateTime class has two overloaded parse() methods to convert time in string to local time instance.

parse(CharSequence text)	//1

parse(CharSequence text, DateTimeFormatter formatter)	//2
  • Use first method if the string contains time in ISO_ZONED_DATE_TIME pattern i.e. 2019-03-28T10:15:30+01:00[Europe/Paris]. This is default pattern.
  • For any other date-time pattern, we need to use second method where we pass the date-time as string as well as a formatter which represents the pattern of that date-time string.
//1 - default pattern
String timeStamp = "2019-03-27T10:15:30";
ZonedDateTime localTimeObj = ZonedDateTime.parse(time);

//2 - specified pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
String timeStamp1 = "2019-03-27 10:15:30 AM";
ZonedDateTime localTimeObj1 = ZonedDateTime.parse(timeStamp1, formatter);

4. How to format ZonedDateTime to string

Use ZonedDateTime.format(DateTimeFormatter) method to format a local time to a desired string representation.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");

ZonedDateTime now = ZonedDateTime.now();

String dateTimeString = now.format(formatter);	//2019-03-28 14:47:33 PM

5. How to modify ZonedDateTime

ZonedDateTime provides below methods for modifications. All methods return a new instance of ZonedDateTime because existing instance is always immutable.

  • plusYears()
  • plusMonths()
  • plusDays()
  • plusHours()
  • plusMinutes()
  • plusSeconds()
  • plusNanos()
  • minusYears()
  • minusMonths()
  • minusDays()
  • minusHours()
  • minusMinutes()
  • minusSeconds()
  • minusNanos()
ZonedDateTime now = ZonedDateTime.now();

//3 hours later
ZonedDateTime zonedDateTime1 = now.plusHours(3);	

//3 minutes earliar
ZonedDateTime zonedDateTime2 = now.minusMinutes(3);

//Next year same time
ZonedDateTime zonedDateTime2 = now.plusYears(1);

//Last year same time
ZonedDateTime zonedDateTime2 = now.minusYears(1);

6. More Examples

Format ZonedDateTime to String
Parse string to ZonedDateTime
Convert LocalDate to ZonedDateTime in Java
ZonedDateTime timezone conversion example
Compare ZonedDateTime instances
Java 8 – Parse string to date-time in UTC

Drop me your questions related to Java 8 ZonedDateTime class in comments.

Happy Learning !!

Ref : ZonedDateTime Java Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces