该方法在两个本地日期实例 之间创建一个流,并允许我们选择指定步长。此方法有两种变体,第一个采用结束 日期 并给出当前日期和结束日期之间的日期列表,而第二个采用Period 对象作为参数,该方法提供一种跳过日期 并仅流式传输所选子集的方法开始 日期和结束 日期之间的日期。LocalDate.datesUntil()
public Stream<LocalDate> datesUntil(LocalDate end) public Stream<LocalDate> datesUntil(LocalDate end, Period step)
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 Stream<LocalDate> daysUntil = 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); } }输出结果
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