
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get dates using LocalDate.datesUntil() method in Java 9?
In this article, we will learn to get dates using the LocalDate.datesUntil() method in Java 9. We will learn about date ranges, skip or limit results and by using the Period class we will apply the custom steps.
What is a LocalDate?
LocalDate is a part of java.time package and is an immutable, date-time object, which often represents a date in the "year-month-day" format. For example, the value of "2nd May 2025" could be stored in a LocalDate.
The following are the maximum and minimum values of the LocalDate:
-
MAX: The maximum supported LocalDate, '+999999999-12-31'.
- MIN: The minimum supported LocalDate, '-999999999-01-01'.
The datesUntil() Method
The LocalDate.datesUntil() method creates a stream between two local date instances and allows us to optionally specify a step size.
This method has two variations:
- The first one takes an end date and gives a list of dates between the current date and the end date.
- The second one takes 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.
Syntax
The following is the syntax for the LocalDate.datesUntil() method:
public Stream<LocalDate> datesUntil(LocalDate end) public Stream<LocalDate> datesUntil(LocalDate end, Period step)
Getting Dates Using LocalDate.datesUntil() Method
Creates two LocalDate objects named "myBirthday" and "christmas". In the "myBirthday" variable, we store the date as "August 8, 1980", and in the "christmas" variable, we store the date as "December 25, 1980".
final LocalDate myBirthday = LocalDate.of(1980, Month.AUGUST, 8); final LocalDate christmas = LocalDate.of(1980, Month.DECEMBER, 25);
The datesUntil(Christmas) generates a stream of all dates from August 8 to December 24, stores it in the "daysUntil" variable. The skip() method ignores the first 50 days. The limit() method takes the next 10 days and uses the forEach loop to print the dates.
final Stream daysUntil = myBirthday.datesUntil(christmas); daysUntil.skip(50).limit(10).forEach(System.out::println);
The datesUntil(christmas, Period.ofMonths(1)) generates a stream of dates in 1-month increments, which is stored in the "monthsUntil" variable. The limit() method takes the first 5 months, and a forEach loop is used to print these dates.
final Stream monthsUntil = myBirthday.datesUntil(christmas, Period.ofMonths(1)); monthsUntil.limit(5).forEach(System.out::println);
Java Code to Get Dates using LocalDate.datesUntil() Method
Below is an example to get dates using the LocalDate.datesUntil() method in Java 9:
import java.time.LocalDate; import java.time.Period; import java.time.Month; import java.util.stream.Stream; public class DatesUntilMethodTest { public static void main(String args[]) { final LocalDate myBirthday = LocalDate.of(1980, Month.AUGUST, 8); final LocalDate christmas = LocalDate.of(1980, Month.DECEMBER, 25); System.out.println("Day-Stream:\n"); final StreamdaysUntil = myBirthday.datesUntil(christmas); daysUntil.skip(50).limit(10).forEach(System.out::println); System.out.println("\nMonth-Stream:\n"); final Stream monthsUntil = myBirthday.datesUntil(christmas, Period.ofMonths(1)); monthsUntil.limit(5).forEach(System.out::println); } }
Output
Day-Stream: 1980-09-27 1980-09-28 1980-09-29 1980-09-30 1980-10-01 1980-10-02 1980-10-03 1980-10-04 1980-10-05 1980-10-06 Month-Stream: 1980-08-08 1980-09-08 1980-10-08 1980-11-08 1980-12-08