Why LocalDate.plusDays not working here?

99封情书 提交于 2019-12-11 02:02:37

问题


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.


回答1:


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

dateFrom = dateFrom.plusDays(1L);



回答2:


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

dateFrom = dateFrom.plusDays(1L);


来源:https://stackoverflow.com/questions/43933397/why-localdate-plusdays-not-working-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!