Gus*_*rya 5 java datetime date jodatime
在我的代码中,我需要使用Joda在一系列日期之间进行迭代,我已经尝试过:
for(LocalDate currentdate = startDate; currenDate.isBefore(endDate); currenDate= currenDate.plusDays(1)){
System.out.println(currentdate);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码正在运行,但迭代currenDate在前一天停止endDate.我想要实现的是迭代在currentDate完全相同时停止endDate.
for(Date currentdate = startDate; currentdate <= endDate; currentdate++){
System.out.println(currentdate );
}
Run Code Online (Sandbox Code Playgroud)
我知道上面的代码是不可能的,但我这样做是为了说明我想要的东西.
小智 8
实际上有一个简单的方法来处理您发布的原始代码,请参阅下面的实现,只是修改了for循环实现:
//test data
LocalDate startDate = LocalDate.now(); //get current date
LocalDate endDate = startDate.plusDays(5); //add 5 days to current date
System.out.println("startDate : " + startDate);
System.out.println("endDate : " + endDate);
for(LocalDate currentdate = startDate;
currentdate.isBefore(endDate) || currentdate.isEqual(endDate);
currentdate= currentdate.plusDays(1)){
System.out.println(currentdate);
}
Run Code Online (Sandbox Code Playgroud)
下面是输出(相对于我的localDate):
startDate:2015-03-26
endDate:2015-03-31
2015-03-26
2015-03-27
2015-03-28
2015-03-29
2015-03-30
2015-03-31
希望这可以帮助!干杯.:)
| 归档时间: |
|
| 查看次数: |
4356 次 |
| 最近记录: |