LocalDate.datesUntil()方法建立了兩個本地日期之間的流
instances 方法可讓我們選擇性地指定步長。此方法有兩種變體,第一種接受end date 作為參數,並傳回目前日期和結束日期之間的日期清單;而第二種接受一個Period 物件作為參數,該物件提供了一種跳過日期的方式,並僅串流start 和end 日期之間的一部分日期。<strong>public Stream<LocalDate> datesUntil(LocalDate end) public Stream<LocalDate> datesUntil(LocalDate end, Period step)</strong>
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); } }
<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>
以上是如何使用Java 9中的LocalDate.datesUntil()方法取得日期?的詳細內容。更多資訊請關注PHP中文網其他相關文章!