I am using a Datepicker in my application. What I am trying to do is getting the date already set in the text field and showing that on Datepicker control. But I am getting NumberFormatException when I use
Integer.parseInt(splittedDate[2]) //i.e the year column.
When I see while debugging splittedDate containf three values which are fine. but while parsing year column at last index throws exception. I am unable to understand why is it so. pleasehelp me out of this. Following is the implementation of CreateDialog method for Datepicker.
@Override
protected Dialog onCreateDialog(int id) {
if (id == DATE_DIALOG_ID) {
if (birthDate.getText().toString().equalsIgnoreCase(""))
return new DatePickerDialog(this, mDateSetListener, mYear,
mMonth, mDay);
else {
String[] splittedDate = birthDate.getText().toString().split("-");
try{
Log.i("Month String", splittedDate[0]);
mMonth = Integer.parseInt(splittedDate[0]);
Log.i("Day String", splittedDate[1]);
mDay = Integer.parseInt(splittedDate[1]);
Log.i("Year String", splittedDate[0]);
mYear = Integer.parseInt(splittedDate[2]);
}
catch(Exception e)
{
Log.e("Number Format Exception Message: ",e.getMessage());
}
return new DatePickerDialog(this, mDateSetListener, mYear,
mMonth, mDay);
}
return null;
}
Here is the logCat info:
09-05 16:21:54.722: E/AndroidRuntime(28596): FATAL EXCEPTION: main
09-05 16:21:54.722: E/AndroidRuntime(28596): java.lang.NumberFormatException: unable to parse '1970 ' as integer
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parse(Integer.java:383)
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parseInt(Integer.java:372)
09-05 16:21:54.722: E/AndroidRuntime(28596): at java.lang.Integer.parseInt(Integer.java:332)
09-05 16:21:54.722: E/AndroidRuntime(28596): at com.test.project.activity.DetailActivity.onCreateDialog(DetailActivity.java:332)
Post the code where you are inserting values in
birthDatefield. Check that there should not be anyblank spaces. There should be digits only. from the Exception, it seems that'1970 'contains a blank space. double check the birthDate field..