Wednesday, May 17, 2017

Java 8 Date and Time API: ZonedDateTime


Java 8 date and time API introduces a new class ZonedDateTime under java.time package which represents a local date and time with a timezone. ZonedDateTime instances are immutable (cannot be changed) so any operations performed on ZonedDateTime will always return a new instance.

In this tutorial, you will learn:
  • How to create a ZonedDateTime instance representing the current date and time with a timezone
  • How to create a ZonedDateTime instance from a given LocalDate,  LocalTime and timezone
  • How to access the ZonedDateTime properties (e.g. month, year, day of week, etc.)
  • How to perform plus and minus operations on ZonedDateTime
  • How to create instance of ZoneId using offset and timezone location
Example
package com.melody.datetime;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeDemo {

 public static void main(String[] args) {
  ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
  System.out.println("Current date and time with timezone: "
    + currentZonedDateTime);

  // Create a ZonedDateTime instance from a given date,
  // time and timezone
  ZoneId zoneId = ZoneId.systemDefault();
  LocalDate localDate = LocalDate.of(2017, 11, 14);
  LocalTime localTime = LocalTime.of(13, 30, 44);
  ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, zoneId);
  System.out.println("ZonedDateTime instance from date and time using system timezone: "
    + zonedDateTime);

  // Access the ZonedDateTime properties
  System.out.println("Get date: " + zonedDateTime.toLocalDate());
  System.out.println("Get time: " + zonedDateTime.toLocalTime());
  System.out.println("Get timezone: " + zonedDateTime.getZone());
  System.out.println("Get year: " + zonedDateTime.getYear());
  System.out.println("Get month (enum): " + zonedDateTime.getMonth());
  System.out.println("Get month: " + zonedDateTime.getMonthValue());
  System.out.println("Get day of month: " + zonedDateTime.getDayOfMonth());
  System.out.println("Get hour: " + zonedDateTime.getHour());
  System.out.println("Get minute: " + zonedDateTime.getMinute());
  System.out.println("Get second: " + zonedDateTime.getSecond());

  // Add and minus operations on ZonedDateTime
  // using Period instead of plusXX or minusXX because it can
  // give more accurate calculation when daylight saving changes
  // are involved.
  System.out.println("Add 1 yr to 2017-11-14: " + zonedDateTime.plus(Period.ofYears(1)));
  System.out.println("Minus 1 yr, 1 month & 1 day from 2017-11-14: " + zonedDateTime.minus(Period.of(1, 1, 1)));

  // Create instance of ZoneId using offset
  ZoneId zoneId1 = ZoneId.of("UTC+8");
  System.out.println("Zone Id using offset UTC+8: " + zoneId1);

  // Create instance of ZoneId using timezone location (continent/city)
  ZoneId zoneId2 = ZoneId.of("Asia/Shanghai");
  System.out.println("Zone Id using timezone location Asia/Shanghai: " + zoneId2);
 }
}

Output

Current date and time with timezone: 2017-05-17T20:34:02.561+08:00[Asia/Shanghai]
ZonedDateTime instance from date and time using system timezone: 2017-11-14T13:30:44+08:00[Asia/Shanghai]
Get date: 2017-11-14
Get time: 13:30:44
Get timezone: Asia/Shanghai
Get year: 2017
Get month (enum): NOVEMBER
Get month: 11
Get day of month: 14
Get hour: 13
Get minute: 30
Get second: 44
Add 1 yr to 2017-11-14: 2018-11-14T13:30:44+08:00[Asia/Shanghai]
Minus 1 yr, 1 month & 1 day from 2017-11-14: 2016-10-13T13:30:44+08:00[Asia/Shanghai]
Zone Id using offset UTC+8: UTC+08:00
Zone Id using timezone location Asia/Shanghai: Asia/Shanghai



No comments:

Post a Comment