2016-07-25 8 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

あなたは何が必要ですか? 2つの日付間の日数ですか?または日付を確認するには?はいの場合、何に対して? –

+0

私は最初から最後まで、選択した日付の範囲を取得する必要があります。私はJUnitテストを書きました。開始日と終了日は正常に動作しますが、中間日を取得しようとすると動作しません。たとえば、7月20日から7月25日を選択すると、日付が20,21,22,23,24、および25となります。 – Void

+0

私はあなたに 'fromDate'と' toDate'を間違った方法で持っていると信じています。 –

答えて

0

fromDate.after(CAL) - truefromDate場合が返されますがbeforecal、同じようなことの後になります。

あなたはCALが2つの日付の間であることを確認したい場合は

は使用:

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

関連する問題