Home > Java > javaTutorial > body text

How to get date using LocalDate.datesUntil() method in Java 9?

PHPz
Release: 2023-08-22 12:45:03
forward
1607 people have browsed it

如何使用Java 9中的LocalDate.datesUntil()方法获取日期?

LocalDate.datesUntil() method creates a stream between two local dates

instances Method allows us to optionally specify the step size. There are two variants of this method, the first one accepts end date as parameter and returns a list of dates between the current date and the end date; while the second one accepts a ## A #Period object is taken as a parameter, which provides a way to skip dates and only stream a portion of the dates between the start and end dates. Syntax

<strong>public Stream<LocalDate> datesUntil(LocalDate end)
public Stream<LocalDate> datesUntil(LocalDate end, Period step)</strong>
Copy after login

Example

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 = <strong>LocalDate.of</strong>(1980, Month.AUGUST, 8);
      final LocalDate christmas = <strong>LocalDate.of</strong>(1980, Month.DECEMBER, 25);

      System.out.println("Day-Stream:\n");
      final <strong>Stream<LocalDate></strong> daysUntil = myBirthday.<strong>datesUntil</strong>(christmas);
      daysUntil.<strong>skip</strong>(50).<strong>limit</strong>(10).<strong>forEach</strong>(System.out::println);

      System.out.println("\nMonth-Stream:\n");
      final Stream monthsUntil = myBirthday.<strong>datesUntil</strong>(christmas, <strong>Period.ofMonths</strong>(1));
      monthsUntil.<strong>limit</strong>(5).forEach(System.out::println);
   }
}
Copy after login

Output

<strong>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
</strong>
Copy after login

The above is the detailed content of How to get date using LocalDate.datesUntil() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!