2012-12-03 3 views
0

종료 날짜 [textbox]가 시작 날짜 [textbox]보다 큰지 안드로이드에서 확인/검증합니까? 내가 시작 및 종료 날짜가 변경됩니다 제가 실행 시간에 일 초기 시간에 현재 날짜 ..를 표시 한안드로이드의 텍스트보기로 시작 날짜와 종료 날짜 유효성 확인

.. 시작 날짜를 변경하는 동안

이 종료 날짜가 시작 날짜보다 커야합니다 ...

도와주세요.

+3

시작일과 종료일을 비교하여 ??? – noob

답변

6

이 기능을 사용해보세요.

public static boolean isDateAfter(String startDate,String endDate) 
    { 
     try 
     { 
      String myFormatString = "yyyy-M-dd"; // for example 
      SimpleDateFormat df = new SimpleDateFormat(myFormatString); 
      Date date1 = df.parse(endDate)); 
      Date startingDate = df.parse(startDate); 

      if (date1.after(startingDate)) 
       return true; 
      else 
       return false; 
     } 
     catch (Exception e) 
     { 

      return false; 
     } 
    } 

enddate가 시작 날짜 이후이면 true를 반환합니다. 귀하의 요구 사항에 따라 날짜 형식을 변경할 수 있습니다.

+0

대단히 감사합니다. 그리고 DatePicker를 사용하여 응용 프로그램의 날짜를 변경했습니다 ... 종료 날짜를 시작 날짜보다 짧게 사용하면 경고가 발생합니다 .. 할 수 있습니까? – gowri

+0

@Gowrishankar datepicker를 사용하여 데이터를 변경하고 textview로 설정하기 전에이 함수에서 전달 및 시작하고 종료해야하며 funtion에 의해 반환 값에 따라 사용자에게 메시지를 표시해야합니다. –

+0

Chirag Raval에게 감사드립니다. – gowri

0
private void initViewValue(){ 
    newCalendar = Calendar.getInstance(); 
    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US); 

} 
private void initViewEvents(){ 

    btn_start_date.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      fromDate(); 
     } 
    }); 

    btn_end_date.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      toDate(); 
     } 
    }); 

} 





private String changeDate(String strStartDate){ 
    SimpleDateFormat readFormat = null, writeFormat = null; 
    Date startDate = null, endDate = null; 
    // String strStartDate = "2014-12-09"; 

    readFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    writeFormat = new SimpleDateFormat("MM/dd/yyyy"); 

    try { 
     startDate = readFormat.parse(strStartDate); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    return writeFormat.format(startDate); 
} 





private void fromDate(){ 
    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      Calendar newDate = Calendar.getInstance(); 
      newDate.set(year, monthOfYear, dayOfMonth); 
      et_start_date.setText(dateFormatter.format(newDate.getTime())); 

      getFromDate = et_start_date.getText().toString().trim(); 
     } 
    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 
    fromDatePickerDialog.show(); 
} 
private void toDate(){ 
    toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      Calendar newDate = Calendar.getInstance(); 
      newDate.set(year, monthOfYear, dayOfMonth); 
      et_end_date.setText(dateFormatter.format(newDate.getTime())); 
      newDate.add(Calendar.DAY_OF_MONTH, 30); 

     } 

    }, 
      newCalendar.get(Calendar.YEAR), newCalendar.get(`enter code here`Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); 

    toDatePickerDialog.getDatePicker().setMinDate(milliseconds(changeDate(getFromDate))); 
    toDatePickerDialog.show(); 
} 

public static long milliseconds(String date) 
{ 
    //String date_ = date; 
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US); 
    System.out.println("Date in milli :: " + date); 
    try 
    { 
     Date mDate = sdf.parse(date); 
     long timeInMilliseconds = mDate.getTime(); 
     System.out.println("Date in milli :: " + timeInMilliseconds); 
     return timeInMilliseconds; 
    } 
    catch (ParseException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return 0; 
} 
관련 문제