2013-04-30 80 views
0

我有兩個日期選擇器進入兩個單獨的編輯文本框。一個是出發日期,另一個是回程日期。我需要一個驗證的理論,將確保返回日期必須在出發日期後..需要驗證退貨日期是否大於出發日期?

感謝您的幫助

這裏是我的代碼:

DepartDate = (TextView) findViewById(R.id.editText1); 
      ReturnDate = (TextView) findViewById(R.id.editText2); 

public void selectDate(View view) { // Function used to set the date 
     switch(view.getId()) { 
     case R.id.imageButton1: // Using the first image button to call the first date picker. 
      DialogFragment newFragment1 = new SelectDateFragment(0); // Giving an index to the date picker so it won't overwrite the textfields 
      newFragment1.show(getSupportFragmentManager(), "DatePicker"); 
      break; 

     case R.id.imageButton2: // Using the first image button to call the first date picker. 
      DialogFragment newFragment2 = new SelectDateFragment(1); // Giving an index to the date picker so it won't overwrite the textfields 
      newFragment2.show(getSupportFragmentManager(), "DatePicker"); 
      break; 
     } 

    } 

    public void populateSetDate(int year, int month, int day) { // Setting the format of the date and setting where the selected date will be entered to 
     DepartDate = (TextView) findViewById(R.id.editText1); 
     DepartDate.setText(month + "/" + day + "/" + year); //Setting the format in which the date will be shown in the textview 

    } 

    public void populateSetDate1(int year1, int month1, int day1) { 

     ReturnDate = (TextView) findViewById(R.id.editText2); 
     ReturnDate.setText(month1 + "/" + day1 + "/" + year1); 

    } 

    public class SelectDateFragment extends DialogFragment implements 
      DatePickerDialog.OnDateSetListener { 

     int type; 
     public SelectDateFragment(int type) { 
      this.type = type; 
     } 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      final Calendar calendar = Calendar.getInstance(); 
      int yy = calendar.get(Calendar.YEAR); 
      int mm = calendar.get(Calendar.MONTH); 
      int dd = calendar.get(Calendar.DAY_OF_MONTH); 
      return new DatePickerDialog(getActivity(), this, yy, mm, dd); 
     } 

     public void onDateSet(DatePicker view, int yy, int mm, int dd) { 
      if(type == 0) { //If the first date picker was clicked then call the following function 
       populateSetDate(yy, mm + 1, dd); 
      } else if(type == 1) { //If the second date picker was clicked then call the following function 
       populateSetDate1(yy, mm + 1, dd); 
      } 
     } 

我也有一個按鈕與onClickListener。如果返回日期大於出發日期,我會希望使按鈕不可點擊。

+0

你可以嘗試這樣的事情,正如我已經張貼在thsi問題http://stackoverflow.com/questions/15038015/how-to-ignore-time-while-checking-date-using-beforedate- d – Pragnani 2013-04-30 17:44:34

回答

0

有一個這個問題的答案在這裏: Date Comparison using Java

,但你還可以設置按鈕,如果日期不通過你的驗證來未啓用。

ImageButton button = (ImageButton)findViewById(R.id.imageButton1); 
if(!passesValidation) { 
    button.setEnabled(false); 
} 

希望這對幫您嗎?

1

我已經使用這個類,你可以作爲一個屬性使用:

[AttributeUsage(AttributeTargets.Property)] 
public class DateGreaterThanAttribute : ValidationAttribute 
{ 
    public DateGreaterThanAttribute(string dateToCompareToFieldName) 
    { 
     DateToCompareToFieldName = dateToCompareToFieldName; 
    } 

    private string DateToCompareToFieldName { get; set; } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 


     DateTime earlierDate = (DateTime)validationContext.ObjectType.GetProperty(DateToCompareToFieldName).GetValue(validationContext.ObjectInstance, null); 


     if ((DateTime)value != null) 
     { 
      DateTime? laterDate = (DateTime)value; 
      if (earlierDate <= laterDate) 
      { 
       return ValidationResult.Success; 
      } 
      else 
      { 
       return new ValidationResult("End date must be later than start date!"); 
      } 

     } 
     else {return new ValidationResult("End date is expected.");} 


    } 
} 

然後,您可以使用它作爲您的datefields註解。

0

請在下面找到java代碼。我希望這對你有幫助。

public class ValidateDate{ 

    private Pattern pattern; 
    private Matcher matcher; 

    private static final String PATTERN_OF_DATE = 
      "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)"; 

    public ValidateDate(){ 
     pattern = Pattern.compile(PATTERN_OF_DATE); 
    } 

    public boolean DateValidate(final String date){ 

    matcher = pattern.matcher(date); 

    if(matcher.matches()){ 

    matcher.reset(); 

    if(matcher.find()){ 

      String dayIs = matcher.group(1); 
     String monthIs = matcher.group(2); 
     int year = Integer.parseInt(matcher.group(3)); 

     if (dayIs.equals("31") && 
      (monthIs.equals("4") || monthIs .equals("6") || monthIs.equals("9") || 
        monthIs.equals("11") || monthIs.equals("04") || monthIs .equals("06") || 
        monthIs.equals("09"))) { 
      return false; // only 1,3,5,7,8,10,12 has 31 days 
     } else if (monthIs.equals("2") || monthIs.equals("02")) { 
        //leap year 
      if(year % 4==0){ 
       if(dayIs.equals("30") || dayIs.equals("31")){ 
        return false; 
       }else{ 
        return true; 
       } 
      }else{ 
       if(dayIs.equals("29")||dayIs.equals("30")||dayIs.equals("31")){ 
        return false; 
       }else{ 
        return true; 
       } 
      } 
      }else{     
     return true;     
      } 
     }else{ 
       return false; 
     }   
    }else{ 
     return false; 
    }    
    } 
}