惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
腾讯CDC
J
Java Code Geeks
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - Franky
博客园 - 聂微东
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
雷峰网
雷峰网
B
Blog RSS Feed
博客园_首页
量子位
F
Fortinet All Blogs
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog

博客园 - zhh

Cesium 河道形状 Cesium 实现 FlowMap Unity3D c# 使对象物体始终面向摄像机 Cesium For Unity Convert WGS84 to Earth Centered Earth Fixed 小流域设计洪水计算 cesium自定义st TerrainProviderEdit Redis Server监控数据采集 cesium添加过道corridor Cesium:entity闪烁(点、面以及billboard) arcgis 3.x 打印 CAS Client集群环境的Session问题及解决方案 不能退出登录 Client Server集群模式下session问题[转] quartz数据不完整导致不调度并且报错Couldn‘t store trigger arcgis popup关闭事件 arcgis 叠加图片 watch arcgis 叠加图片 三维地图3DGIS平台技术指标要求规划 太阳方位角
Java – Get All Dates Between Two Dates
zhh · 2021-11-03 · via 博客园 - zhh

Learn to get all the dates between the two given dates. We will see the solutions in Java 7, Java 8, and Java 9.

1. LocalDate.datesUntil() – Java 9

LocalDate‘s datesUntil() method returns a sequential ordered stream of dates. The returned stream starts from startDate and goes to endDate (exclusive) by an incremental step of 1 day.

All dates between two given dates

LocalDate startDate = LocalDate.now();

LocalDate endDate = startDate.plusMonths(2);

List<LocalDate> listOfDates = startDate.datesUntil(endDate)

                            .collect(Collectors.toList());

System.out.println(listOfDates.size());    

2. Stream.iterate() – Java 8

To get all dates, create a stream of dates adding adding 1 to startdate and so on, until end date.

All dates between two given dates

LocalDate startDate = LocalDate.now();

LocalDate endDate = startDate.plusMonths(2);

long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);

List<LocalDate> listOfDates1 = Stream.iterate(startDate, date -> date.plusDays(1))

                                    .limit(numOfDays)

                                    .collect(Collectors.toList());

System.out.println(listOfDates.size());    

The same kind of stream can be obtained in multiple ways. For example, LongStream is one of those ways.

All dates between two given dates

LocalDate startDate = LocalDate.now();

LocalDate endDate = startDate.plusMonths(2);

long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);

List<LocalDate> listOfDates2 = LongStream.range(0, numOfDays)

                                .mapToObj(startDate::plusDays)

                                .collect(Collectors.toList());

System.out.println(listOfDates.size());    

3. Create dates in while loop – Java 7

To get all dates from startDate to endDate in Java 7, we must write our own custom logic as their is very limited API support in JDK. Or else, consider using 3rd party libraries e.g. Joda-time.

All dates between two given dates

Date todaysDate = new Date();

Date anotherDate = new Date(todaysDate.getTime() + 61*24*60*60*1000l);

List<Date> listOfDates3 = getDaysBetweenDates(todaysDate, anotherDate);

System.out.println(listOfDates3.size());       

public static List<Date> getDaysBetweenDates(Date startdate, Date enddate)

{

    List<Date> dates = new ArrayList<Date>();

    Calendar calendar = new GregorianCalendar();

    calendar.setTime(startdate);

    while (calendar.getTime().before(enddate))

    {

        Date result = calendar.getTime();

        dates.add(result);

        calendar.add(Calendar.DATE, 1);

    }

    return dates;

}

Drop me your questions related to get the list of all dates between two dates in Java.

Happy Learning !!