Friday, September 29, 2017

Java 8 Date And Time API:Time Zones

In the previous three posts, we took a detailed look at the Java 8 Local Date and Time classes. In this post, we will take a look at the Java 8 provides ZonedDateTime and OffsetDateTime classes.

ZonedDateTime

ZonedDateTime uses ZoneId to represent different time zones. The following example shows how to create a ZoneId for Chicago
ZoneId zoneId = ZoneId.of("America/Chicago");
Following piece of code shows how to obtain a list of all zone ids.
Set zoneIdList = ZoneId.getAvailableZoneIds();
There are many ways to instantiate ZonedDateTime, most of them parallel LocalDateTime as shown in the previous post, with the addition of Zone Id.
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
ZonedDateTime.parse("2015-05-03T10:15:30+01:00[America/Los_Angeles]");
Truncate Time from ZonedDateTime
The truncateTo() method can be used to truncate the time fields. Any Time Unit lesser than the passed parameter will be marked to zero.
// Anything under days is set to zero. Works only for time.
System.out.println(zonedDateTime + " Truncated to  " + zonedDateTime.truncatedTo(ChronoUnit.HOURS));
Convert Time Zone
Converting time from one timezone to another is one of the common requirements. The method of ZonedDateTime can be used to convert a given ZonedDateTime to any timzone. The following example can be used to convert the current time in Chicago timezone to time in Los_Angeles
// Convert Time Zone
System.out.println(zonedDateTime + " in Los Angeles " + zonedDateTime.withZoneSameInstant(ZoneId.of("America/Los_Angeles")));
ZonedDateTime also offers many of the same utility methods offered by LocalDateTime. A few examples are shown below
System.out.println("3 days before today is : " + zonedDateTime.minus(3, ChronoUnit.DAYS));
System.out.println("3 decades before today is : " + zonedDateTime.minus(3, ChronoUnit.DECADES));
System.out.println("3 days after today is : " + zonedDateTime.plus(3, ChronoUnit.DAYS));
System.out.println("3 decades after today is : " + zonedDateTime.plus(3, ChronoUnit.DECADES));

System.out.println("The day of the week is : " + zonedDateTime.getDayOfWeek());
System.out.println("The day of the year is : " + zonedDateTime.getDayOfYear());

OffsetDateTime

OffsetDateTime class can be used to represent DateTime with an Offset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00" can be stored in an OffsetDateTime. OffsetDateTime can be created in the following ways.
// Now
OffsetDateTime offsetDateTime = OffsetDateTime.now();
System.out.println("OffsetDateTime : " + offsetDateTime);

// Get LocalDateTime and apply Offset
LocalDateTime localDateTime = LocalDateTime.of(2017, Month.SEPTEMBER, 29, 5, 30);
ZoneOffset offset = ZoneOffset.of("+02:00");

OffsetDateTime offSetByTwo = OffsetDateTime.of(localDateTime, offset);
System.out.println(localDateTime + " Offset by two " + offSetByTwo);

Full code for this post

import java.time.LocalDateTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class ZonedDateTimeExamples {

 public static void main(String[] args) {
  ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/Chicago"));
  System.out.println(zonedDateTime);

  // Convert Time Zone
  System.out
    .println(zonedDateTime + " in Los Angeles " + zonedDateTime.withZoneSameInstant(ZoneId.of("America/Los_Angeles")));

  // Anything under days is set to zero. Works only for time.
  System.out.println(zonedDateTime + " Truncated to  " + zonedDateTime.truncatedTo(ChronoUnit.HOURS));

  System.out.println("3 days before today is : " + zonedDateTime.minus(3, ChronoUnit.DAYS));
  System.out.println("3 decades before today is : " + zonedDateTime.minus(3, ChronoUnit.DECADES));
  System.out.println("3 days after today is : " + zonedDateTime.plus(3, ChronoUnit.DAYS));
  System.out.println("3 decades after today is : " + zonedDateTime.plus(3, ChronoUnit.DECADES));

  System.out.println("The day of the week is : " + zonedDateTime.getDayOfWeek());
  System.out.println("The day of the year is : " + zonedDateTime.getDayOfYear());

  // Now
  OffsetDateTime offsetDateTime = OffsetDateTime.now();
  System.out.println("OffsetDateTime : " + offsetDateTime);

  // Get LocalDateTime and apply Offset
  LocalDateTime localDateTime = LocalDateTime.of(2017, Month.SEPTEMBER, 29, 5, 30);
  ZoneOffset offset = ZoneOffset.of("+02:00");

  OffsetDateTime offSetByTwo = OffsetDateTime.of(localDateTime, offset);
  System.out.println(localDateTime + " Offset by two " + offSetByTwo);

 }

}

No comments:

Post a Comment

Popular Posts