首页 > Java > java教程 > 正文

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

PHPz
发布: 2023-08-22 12:45:03
转载
1607 人浏览过

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

LocalDate.datesUntil()方法创建了两个本地日期之间的流

instances 方法允许我们可选地指定步长。该方法有两种变体,第一种接受end date 作为参数,并返回当前日期和结束日期之间的日期列表;而第二种接受一个Period 对象作为参数,该对象提供了一种跳过日期的方式,并仅流式传输start end 日期之间的一部分日期。

语法

<strong>public Stream<LocalDate> datesUntil(LocalDate end)
public Stream<LocalDate> datesUntil(LocalDate end, Period step)</strong>
登录后复制

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);
   }
}
登录后复制

输出

<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中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!