Monday, October 02, 2017

Java 9 Date Time API Changes

Java 9 added a few enhancements to the new Date and Time API which was introduced in Java 8. We will go over a few of these additions to LocalDate and LocalTime in this post
  • Stream<LocalDate> datesUntil​(LocalDate endExclusive) // LocalDate
  • public Stream<LocalDate> datesUntil​(LocalDate endExclusive, Period step) //LocalDate
  • public long toEpochSecond​(LocalTime time, ZoneOffset offset) //LocalDate
  • toEpochSecond​(LocalDate date, ZoneOffset offset) //LocalTime

Getting a stream of Dates with datesUntil

The datesUntil() method has two variations, the first one takes end date and gives a list of dates between the current date and end date, while the second one take a Period object as a parameter that provides a way to skip dates and Stream only a select subset of the dates between start and end dates.
For the first example, we will just print all the dates between today and December 31, 2017.
Stream<LocalDate> dates = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"));
dates.forEach(System.out::println);
In the next couple of examples, we will use the second variation or datesUntil method to print alternate days and days apart by a week.
Stream<LocalDate> alternateDays = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"), Period.ofDays(2));
alternateDays.forEach(System.out::println);


Stream<LocalDate> weekly = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"), Period.ofWeeks(1));
weekly.forEach(System.out::println);

Number of milliseconds from epoch of 1970-01-01T00:00:00Z with toEpochSecond()

Since both LocalDate and LocalTime do not contain the time and date respectively, in order to convert them to Epochtime, we have to pass the LocalTime and LocalDate to the toEpochTime. The following two examples show how to use these methods
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();

System.out.println("LocalDate toEpochSecond : " + date.toEpochSecond(time, ZoneOffset.of("Z")));

System.out.println("LocalTime toEpochSecond : " + time.toEpochSecond(date, ZoneOffset.of("Z")));

Full code

package com.aoj.java9.base;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.util.stream.Stream;

public class Java9DateTime {
 public static void main(String[] args) {
  Stream<LocalDate> dates = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"));
  dates.forEach(System.out::println);

  Stream<LocalDate> alternateDays = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"), Period.ofDays(2));
  alternateDays.forEach(System.out::println);

  
  Stream<LocalDate> weekly = LocalDate.now().datesUntil(LocalDate.parse("2017-12-31"), Period.ofWeeks(1));
  weekly.forEach(System.out::println);
  
  
  LocalDate date = LocalDate.now();
  LocalTime time = LocalTime.now();
  
  System.out.println("LocalDate toEpochSecond : " + date.toEpochSecond(time, ZoneOffset.of("Z")));
  
  System.out.println("LocalTime toEpochSecond : " + time.toEpochSecond(date, ZoneOffset.of("Z")));
 }

}

1 comment:

  1. Great post on Java-X Blogspot! The explanations are clear and easy to follow, especially for beginners trying to understand core Java concepts. I really appreciate the practical examples you’ve shared—they make learning much more effective. Looking forward to reading more informative Java tutorials from you.

    Become a certified Abacus Trainer and explore exciting opportunities with our franchise for Abacus programs.

    abacus franchise in india

    ReplyDelete

Popular Posts