OCAJP has a topic similar to this one, here only ZonedDateTime, Duration and Instant are new to OCPJP.
ZonedDateTime |
java.time package has 4 type of date-based and time-based classes:
- Local Date. Contains date - no time and time zone.
- LocalTime. Contains time - no date and timezone.
- LocalDateTime. Contains date and time - no timezone.
- ZonedDateTime. Contains date, time and time zone.
The first 3 we have visited in OCAJP, the 4th one is new. Before telephone is invented, everybody knows "Let's meet again today afternoon at 3" means, because you are talking with a local folk in front of your face. At internet era, you need to be careful who are you talking to -- the guy might be talking to you with skype in India. Where your afternoon is his midnight. In that case, you have to additionally mention the time zone.
OCPJP>cat ZonedDateTimeExample.java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeExample {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("US/Eastern");
LocalDate localDate = LocalDate.of(2017, 2, 2);
LocalTime localTime = LocalTime.of(10, 10, 10);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
//zonedDateTime = ZonedDateTime.of(2017, 2, 2, 10, 10, 10, zoneId); //don't compile
zonedDateTime = ZonedDateTime.of(2017, 2, 2, 10, 10, 10, 10, zoneId);
System.out.println(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
System.out.println(
ZonedDateTime.of(localDate, localTime, ZoneId.of("US/Eastern"))
.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}
}
OCPJP>
OCPJP>javac ZonedDateTimeExample.java
OCPJP>java ZonedDateTimeExample
2017-02-02T10:10:10-05:00[US/Eastern]
2017-02-02T10:10:10.00000001-05:00[US/Eastern]
2017-02-02T10:10:10-05:00[US/Eastern]
OCPJP>
While a Period is a day or more of time, Duration is intended for smaller units of time. The Duration can additionally store hours, minutes and seconds, Millisecond, Nanoseconds.
OCPJP>cat DurationExample.java
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class DurationExample {
public static void main(String...args) {
LocalDate localDate = LocalDate.of(2017, 1, 1);
LocalTime localTime = LocalTime.of(1, 1);
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("US/Eastern"));
System.out.println("Period.of");
Period period = Period.of(1, 1, 1);
//Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Months
//System.out.println(localTime.plus(period));
System.out.println(localDate.plus(period));
System.out.println(localDateTime.plus(period));
System.out.println(zonedDateTime.plus(period));
System.out.println("Duration.ofDays");
Duration duration = Duration.ofDays(1);
//Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
//System.out.println(localDate.plus(duration));
System.out.println(localTime.plus(duration));
System.out.println(localDateTime.plus(duration));
System.out.println(zonedDateTime.plus(duration));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.DAYS)));
System.out.println("Duration.ofHours");
System.out.println(localDateTime.plus(Duration.ofHours(1)));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.HOURS)));
System.out.println("Duration.ofMinutes");
System.out.println(localDateTime.plus(Duration.ofMinutes(1)));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.MINUTES)));
System.out.println("Duration.ofSeconds");
System.out.println(localDateTime.plus(Duration.ofSeconds(1)));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.SECONDS)));
System.out.println("Duration.ofMillis");
System.out.println(localDateTime.plus(Duration.ofMillis(1)));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.MILLIS)));
System.out.println("Duration.ofNanos");
System.out.println(localDateTime.plus(Duration.ofNanos(1)));
System.out.println(localDateTime.plus(Duration.of(1, ChronoUnit.NANOS)));
}
}
OCPJP>
OCPJP>javac DurationExample.java
OCPJP>java DurationExample
Period.of
2018-02-02
2018-02-02T01:01
2018-02-02T01:01-05:00[US/Eastern]
Duration.ofDays
01:01
2017-01-02T01:01
2017-01-02T01:01-05:00[US/Eastern]
2017-01-02T01:01
Duration.ofHours
2017-01-01T02:01
2017-01-01T02:01
Duration.ofMinutes
2017-01-01T01:02
2017-01-01T01:02
Duration.ofSeconds
2017-01-01T01:01:01
2017-01-01T01:01:01
Duration.ofMillis
2017-01-01T01:01:00.001
2017-01-01T01:01:00.001
Duration.ofNanos
2017-01-01T01:01:00.000000001
2017-01-01T01:01:00.000000001
OCPJP>
The Instant class represents a specific moments in time in the GMT time zone.
OCPJP>cat InstantsDemo.java
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class InstantsDemo {
public static void main(String...args) {
LocalDate localDate = LocalDate.of(2017, 1, 1);
LocalTime localTime = LocalTime.of(1, 1);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime, ZoneId.of("US/Eastern") );
System.out.println(zonedDateTime);
System.out.println(zonedDateTime.toInstant());
System.out.println(Instant.ofEpochMilli(0));
Duration duration = Duration.between(Instant.now(), zonedDateTime.toInstant());
System.out.println(duration.toDays());
Instant nextDay = Instant.now().plus(1, ChronoUnit.DAYS);
System.out.println(nextDay);
System.out.println(Instant.now().plus(1, ChronoUnit.HOURS));
System.out.println(Instant.now().plus(1, ChronoUnit.WEEKS));
}
}
OCPJP>
OCPJP>
OCPJP>javac InstantsDemo.java
OCPJP>java InstantsDemo
2017-01-01T01:01-05:00[US/Eastern]
2017-01-01T06:01:00Z
1970-01-01T00:00:00Z
-305
2017-11-04T01:56:41.188Z
2017-11-03T02:56:41.188Z
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks
at java.time.Instant.plus(Instant.java:862)
at InstantsDemo.main(InstantsDemo.java:25)
OCPJP>