Why LocalDate.plusDays not working here?

2019-07-14 06:55发布

I'm trying to split a date range into individual dates in the following way:

private static void splitDates(LocalDate dateFrom, LocalDate dateTo) {
    while (dateFrom.isBefore(dateTo) || dateFrom.isEqual(dateTo)) {
        System.out.println(dateFrom);
        dateFrom.plusDays(1L);
    }
}

And I don't know why dateFrom.plusDays(1L) is not working as the date remains still the same so the loop becomes infinite.

2条回答
forever°为你锁心
2楼-- · 2019-07-14 07:30

Because method plusDays doesn't change variable dateFrom. You should do like this:

dateFrom = dateFrom.plusDays(1L);
查看更多
相关推荐>>
3楼-- · 2019-07-14 07:31

plusDays doesn't alter the original LocalDate, you have to assign the result :

dateFrom = dateFrom.plusDays(1L);
查看更多
登录 后发表回答