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