2013-04-30 3 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- 디 – Pragnani

답변

0

여기이 질문에 대한 답이있다 : Date Comparison using Java

그러나 당신은 또한 날짜가 유효성 검사를 통과하지 않는 경우 버튼을 사용할 수 없습니다 할 설정할 수 있습니다.

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

희망이 도움이 되셨습니까?

[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의 주석으로 사용할 수 있습니다

1

난 당신이 속성으로 사용할 수있는이 클래스를 사용하고 있습니다.

0

아래 자바 코드를 찾으십시오. 나는 그것이 당신에게 도움이되기를 바랍니다.

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; 
    }    
    } 
} 
관련 문제