2013-02-23 89 views
-3

我知道替代方法來檢查下一個日期,但是我想知道在使用之前是否有任何忽略檢查時間的可能性。如何在使用之前檢查日期時忽略時間(日期d)

我有以下代碼

public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     try { 
     if (fired == true) { 
      return; 
     } else { 
      // first time fired 
      fired = true; 
     } 
     String date = checkDigit(monthOfYear + 1) + "/" 
       + checkDigit(dayOfMonth) + "/" + year; 
     strDate = date; 
     SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

     Date d=df.parse(strDate); 
      Calendar cal = Calendar.getInstance(); 
      cal.add(Calendar.DATE, 1); 
     if(d.before(cal.getTime())) 
     { 
      etDOB.setText(strDate); 
     } 
     else 
     { 
      Toast.makeText(context, "Enter Valid Date Of Birth", Toast.LENGTH_SHORT).show(); 
      etDOB.setText(" "); 
     } 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    } 

d.before(cal.getTime()) checking date but if the selected Date is next Date than it also checking time 

實施例:已選 日期值是feb 24 2013所以d包含相同

cal.getTime() also contains feb 24 2013 

但具有時間00:00:00 GMT+5:30第一日期和第二有時間1:00:00 GMT+5:30這導致條件失敗。所以我的問題是如何忽略之前檢查時間。

回答

3

您可以格式化和分析使用dfSimpleDateFormat("MM/dd/yyyy"))從cal.getTime()日起將擺脫時間,然後比較結果爲d

3

雖然createing了日曆對象

Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.DATE, 1); 
cal.set(Calendar.Minute, 0); 
cal.set(Calendar.Second, 0); 

現在你的病情不會失敗

+0

簡單,完成工作:) – Bruce 2016-04-09 06:50:56

相關問題