2016-07-25 119 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) - 將返回true如果fromDate將是cal,類似的事情與before後。

如果你想檢查卡是兩個日期之間使用:

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

相關問題