Java ZonedDateTime class

Java ZonedDateTime class

Java ZonedDateTime class is an immutable representation of a date-time with a time-zone. It inherits Object class and implements the ChronoZonedDateTime interface.

ZonedDateTime class is used to store all date and time fields, to a precision of nanoseconds, and a time-zone with a zone offset used to handle ambiguous local date-times.


Java ZonedDateTime class declaration

Let’s see the declaration of java.time.ZonedDateTime class.

  1. public final class ZonedDateTime extends Object
  2. implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable

Methods of Java ZonedDateTime

Method Description
String format(DateTimeFormatter formatter) It is used to format this date-time using the specified formatter.
int get(TemporalField field) It is used to get the value of the specified field from this date-time as an int.
ZoneId getZone() It is used to get the time-zone, such as ‘Asia/Kolkata’.
ZonedDateTime withZoneSameInstant(ZoneId zone) It is used to return a copy of this date-time with a different time-zone, retaining the instant.
static ZonedDateTime now() It is used to obtain the current date-time from the system clock in the default time-zone.
static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone) It is used to obtain an instance of ZonedDateTime from a local date and time.
ZonedDateTime minus(long amountToSubtract, TemporalUnit unit) It is used to return a copy of this date-time with the specified amount subtracted.
ZonedDateTime plus(long amountToAdd, TemporalUnit unit) It is used to return a copy of this date-time with the specified amount added.

 

Java ZonedDateTime class Example

  1. import java.time.ZonedDateTime;
  2. public class ZonedDateTimeExample{
  3.   public static void main(String[] args) {
  4.     ZonedDateTime zone = ZonedDateTime.parse(“2016-10-05T08:20:10+05:30[Asia/Kolkata]”);
  5.     System.out.println(zone);
  6.   }
  7. }

Output:

2016-10-05T08:20:10+05:30[Asia/Kolkata]

Java ZonedDateTime class Example: of() and withZoneSameInstant()

  1. import java.time.*;
  2. public class ZonedDateTimeExample{
  3.   public static void main(String[] args) {
  4.     LocalDateTime  ldt = LocalDateTime.of(2017, Month.JANUARY,  19,   15,   26);
  5.     ZoneId  india = ZoneId.of(“Asia/Kolkata”);
  6.     ZonedDateTime zone1  = ZonedDateTime.of(ldt, india);
  7.     System.out.println(“In India Central Time Zone: “ + zone1);
  8.     ZoneId  tokyo = ZoneId.of(“Asia/Tokyo”);
  9.     ZonedDateTime zone2   = zone1.withZoneSameInstant(tokyo);
  10.     System.out.println(“In Tokyo Central Time Zone:”  + zone2);
  11.   }
  12. }

Output:

In India Central Time Zone: 2017-01-19T15:26+05:30[Asia/Kolkata]
In Tokyo Central Time Zone:2017-01-19T18:56+09:00[Asia/Tokyo]

Java ZonedDateTime class Example: getZone()

  1. import java.time.ZonedDateTime;
  2. public class ZonedDateTimeExample{
  3.   public static void main(String[] args) {
  4.     ZonedDateTime zone =ZonedDateTime.now();
  5.     System.out.println(zone.getZone());
  6.   }
  7. }

Output:

Asia/Kolkata

Java ZonedDateTime class Example: minus()

  1. import java.time.Period;
  2. import java.time.ZonedDateTime;
  3. public class ZonedDateTimeExample {
  4.   public static void main(String[] args) {
  5.     ZonedDateTime zone= ZonedDateTime.now();
  6.     ZonedDateTime m = zone.minus(Period.ofDays(126));
  7.     System.out.println(m);
  8.   }
  9. }

Output:

2016-09-15T12:54:01.354+05:30[Asia/Kolkata]

Java ZonedDateTime class Example: plus()

  1. import java.time.*;
  2. public class ZonedDateTimeExample {
  3.   public static void main(String[] args) {
  4.     ZonedDateTime zone= ZonedDateTime.now();
  5.     ZonedDateTime p = zone.plus(Period.ofDays(126));
  6.     System.out.println(p);
  7.   }
  8. }

Output:

2017-05-25T12:56:12.417+05:30[Asia/Kolkata]
Next TopicJava ZoneId
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java ZonedDateTime class Java ZonedDateTime class is an immutable representation of a date-time with a time-zone. It inherits Object class and implements the ChronoZonedDateTime interface. ZonedDateTime class is used to store all date and time fields, to a precision of nanoseconds, and a time-zone with a zone offset used to handle ambiguous local date-times. Java […]

Tags:

....
...