Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Date API Improvement

Java has improved its date-time API by adding some new methods to LocalDate class in Java 9 version. Some of these are added to connect with Java stream API. These methods are listed below.

  • public static LocalDate ofInstant(Instant instant, ZoneId zone)
  • public Stream<LocalDate> datesUntil(LocalDate endExclusive)
  • public Stream<LocalDate> datesUntil(LocalDate endExclusive, Period step)
  • public long toEpochSecond(LocalTime time, ZoneOffset offset)

Let's have examples to understand the use of these methods.

1. ofInstant() Method

This method is used to obtain an instance of LocalDate from an Instant and zone ID. The syntax of the method is given below.

public static LocalDate ofInstant(Instant instant, ZoneId zone)

Example:

In this example, we are getting the current date by using ofInstant() method. The systemDefault() method returns the Zone Id of the system.

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

public class Main { 
	public static void main(String[] args){    
		// Create LocalDate 
        LocalDate localdate = LocalDate.ofInstant( 
                Instant.now(), 
                ZoneId.systemDefault()); 
  
        System.out.println("Current Date: "+ localdate);    
	}
}


Current Date: 2020-07-23

Java LocalDate datesUntil() Method

This method is used to get a sequential stream order of dates. The returned stream starts from a date and goes to endDate(which is exclusive) by an incremental step of 1 day.

public Stream<LocalDate> datesUntil(LocalDate endExclusive)

public Stream<LocalDate> datesUntil(LocalDate endExclusive, Period step)

Example:

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

public class Main { 
	public static void main(String[] args){    
		final LocalDate date1 = LocalDate.parse("2012-11-28");
	    final LocalDate date2  = LocalDate.parse("2012-12-05");

	      System.out.println("Day Stream");
	      Stream<LocalDate> daysUntil = date1.datesUntil(date2);
	      daysUntil.forEach(System.out::println);

	      System.out.println("Month-Stream");
	      Stream<LocalDate> monthsUntil = date1.datesUntil(date2,Period.ofMonths(1));
	      monthsUntil.forEach(System.out::println);
	}
}


Day Stream
2012-11-28
2012-11-29
2012-11-30
2012-12-01
2012-12-02
2012-12-03
2012-12-04
Month-Stream
2012-11-28

The toEpochSecond() Method

This method converts this LocalDate to the number of seconds since the epoch of 1970-01-01T00:00:00Z.

public long toEpochSecond(LocalTime time, ZoneOffset offset)

Example:

This method combines the localdate with the specified time and calculates seconds. See the example below.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;

public class Main { 
	public static void main(String[] args){
		// Creating date
		LocalDate date = LocalDate.parse("2012-12-20");
	    System.out.println("Epoch Seconds : " + date.toEpochSecond(LocalTime.now(), ZoneOffset.of("Z")));
	}
}


Epoch Seconds : 1356031765