2016-09-13 4 views
1

plusDays(1)을 사용하는 몇 가지 이유로 나에게 정확한 답을주지 못합니다. 결과를 1 씩 증가시킵니다. 아래에서이 코드를 올바르게 변경하기 위해 변경해야 할 사항. plusDays (1) 제거하는 것은 작동하지 않습니다. 여기서 내가 뭘 잘못하고있는거야.Java에서 두 날짜 사이의 날짜 목록을 다른 결과로 얻으십시오.

그것은 출력

September 13, 2016 
September 14, 2016 
September 15, 2016 

하지

September 14, 2016 
September 15, 2016 
September 16, 2016 

코드합니다 :

String startDate = "2016-09-13"; 
String endDate = "2016-09-15"; 
LocalDate start = LocalDate.parse(startDate); 
LocalDate end = LocalDate.parse(endDate); 
List<LocalDate> totalDates = new ArrayList<>(); 

while (!start.isAfter(end)) { 
    totalDates.add(start); 
    start = start.plusDays(1); 

    Milestones modelMilestones = new Milestones(); 
    modelMilestones .setMilestone(start.toString("MMMM dd, yyyy")); 
    mDataList.add(modelMilestones); 

} 
+0

코드를 디버깅하려 했습니까? – MrSmith42

+1

당신은'plusDays'를 호출 한 후에'toString' *를 호출합니다. 그냥하지 마라.'start = start.plusDays (1); '를 루프의 마지막 줄로 옮긴다. –

+1

사이드 노트. 'totalDates'는 동일한 객체에 대한 여러 참조를 포함합니다. – talex

답변

4

이 한 번 같은 시도 :

while (!start.isAfter(end)) { 
       totalDates.add(start); 
       Milestones modelMilestones = new Milestones(); 
       modelMilestones .setMilestone(start.toString("MMMM dd, yyyy")); 
       mDataList.add(modelMilestones); 

       start = start.plusDays(1); 
      } 
관련 문제