2016-07-25 3 views
0

캘린더 사이에 날짜를 가져오고 싶지만 어떤 이유로 이것이 작동하지 않습니다. 어떤 제안? 감사.캘린더 날짜를 가져 오는 방법은 무엇입니까?

public boolean verifyDate(Calendar cal) {  
    Calendar toDate = Calendar.getInstance(); 
    toDate.setTime(passToDate); 
    Calendar fromDate = Calendar.getInstance(); 
    fromDate.setTime(passFromDate); 
    return !((fromDate.after(cal) && toDate.before(cal)) || DateUtils.isSameDay(fromDate, cal) || DateUtils.isSameDay(toDate, cal)); 
} 
+0

당신은 무엇을해야합니까? 두 날짜 간의 일수? 아니면 날짜를 확인 하시겠습니까? 그렇다면 무엇을 반대합니까? –

+0

처음부터 끝까지 선택한 날짜 범위를 가져와야합니다. JUnit 테스트를 작성하고 시작 날짜와 종료 날짜가 올바르게 작동하지만 중간 날짜를 얻으려고해도 작동하지 않습니다. 예를 들어 7 월 20 일부터 7 월 25 일까지를 선택하면 20, 21, 22, 23, 24, 25 날짜를 고려합니다. – Void

+0

나는 fromDate와 toDate를 잘못 생각합니다. –

답변

0

fromDate.after (CAL) - truefromDate 경우를 반환은 beforecal, 비슷한 일 후가 될 것입니다.

당신이 칼이 두 날짜 사이에 있는지 확인하려면

사용 :

return cal.after(fromDate) && cal.before(toDate) 
    && !(DateUtils.isSameDay(fromDate, cal) || DateUtils.isSameDay(toDate, cal)); 
+0

감사합니다.이 코드는 작동했습니다. – Void

0
public static List<String> getDates(String startDate, String endDate) 
      throws ParseException, java.text.ParseException { 
     List<String> dateList = new ArrayList<String>(); 
     if (startDate == null || startDate.equals("") || endDate == null || endDate.equals("")) { 
      return null; 
     } 


     Calendar calendar = new GregorianCalendar(); 


     DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     Date d1 = dateFormat.parse(startDate); 
     Date d2 = dateFormat.parse(endDate); 

     Calendar calendar2 = new GregorianCalendar(); 
     calendar2.setTime(d2); 
     calendar2.add(Calendar.DATE, 1); 
     calendar.setTime(d1); 

     while (calendar.getTime().before(calendar2.getTime())) 
     { 
      Date result = calendar.getTime(); 
      dateList.add(dateFormat.format(result)); 
      calendar.add(Calendar.DATE, 1); 
     } 


     return dateList; 
    } 
+0

100 % 작동 코드와 재사용 가능한 코드 – Ganesh

관련 문제