1

hi in my application i have a form which contains an edittext, i have used this edittext to enter a date. I used datepicker to do that. And now i am validating the date as if the user cannot enter future date.I have used the following code,

    Calendar objCalendar = Calendar.getInstance();
    editTextDate.setOnClickListener(new View.OnClickListener()
    {
public void onClick(View v) 
    {
new DatePickerDialog(AddExpenses.this, objFromDatePicker,
objCalendar.get(Calendar.YEAR), objCalendar
.get(Calendar.MONTH), objCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
    DatePickerDialog.OnDateSetListener objFromDatePicker = 
    new DatePickerDialog.OnDateSetListener() 
    {   
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) 
    {
Calendar objCalendar = Calendar.getInstance();
objCalendar.set(Calendar.YEAR, year);
objCalendar.set(Calendar.MONTH, monthOfYear);
objCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateFromDate(objCalendar);
}
};
public void updateFromDate(Calendar objCalendar) 
    {
editTextDate.setText(objDateFormat.format(objCalendar.getTime()));
}
    currentDayDate=objCalendar.getTime();
    String editTextData = editTextDateDate.getText().toString();
    SimpleDateFormat objFormat = new SimpleDateFormat("MM/dd/yy");
    Date parsedDate = null;
    String READYTOGOWITHDATE = null;
    try {
    parsedDate = objFormat.parse(editTextData);
    } catch (ParseException e) {
    }
if(parsedDate.after(currentDayDate))
{
    String message="entered date should not exceed current date";
    displayDialog(message);
}
else
{
    ............
}

Everything works fine with my HTC wildfire s,Motorolla Atrix and samsung galaxy tab.But i got complaints from users using huawei phone and Sony ericsson xperia X10 mini that when they are entered current date or any date in the current year, dialog is getting displayed with a message "entered date should not exceed current date". Can i know why is this happening? this problem is not getting replicated in all mobiles, can anyone help me on this?

pappu
  • 23
  • 6

1 Answers1

0

Why can't you simply call setMaxDate()?

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • thank you so much for your reply,i will surely try this,but the above code written by me was fine, and it worked in all the devices i have tested.But,failed in few phones which were mentioned above.What i want to know is,does date validation varies phone by phone? – pappu Mar 19 '12 at 06:36
  • Edit your question to include the code that assigns the values of editTextDate & currentDayDate. – Dheeraj Vepakomma Mar 19 '12 at 06:42
  • and regarding setMaxDate() method, as i am using datepickerdialog, i don't have a method to get an instance of datepicker, although we have getDatePicker() method in DatePickerDialog calss, it is available only from api level 11, but my application have a minsdkversion of 7.So i cannot use it [checked here](http://stackoverflow.com/questions/5163453/max-date-on-an-android-datepickerdialog) – pappu Mar 19 '12 at 07:09
  • Is editTextDate a Calendar or a TextView? In one place editTextDate.setText() is called and in another editTextDate.after() is called. So what type is it?? – Dheeraj Vepakomma Mar 19 '12 at 07:43
  • iam sorry,editTextDate is a EditText object, i have edited the code now – pappu Mar 19 '12 at 12:42
  • i got another user's complaint about the same issue, this time it is in samsung galaxy s, i don't have the device to replicate the issue, in all the available devices i have it is working fine.Can you please help me on this? – pappu Mar 21 '12 at 09:08
  • the date format iam using is MM/dd/yy,and iam using Dateformat class to convert the string to date. – pappu Mar 21 '12 at 09:11