So the month can be in the range [0-11] right?
I dont think this is a good idea. To make it easy for you to parse the string you need to have a defined/constant width for each of the fields so that you can apply the pattern ddMMyyyy
. Otherwise you'll have to find a way to figure out the width of each part in order to correctly parse it, which can be tricky. For example if you have 1112008
, is it January 11, 2008
or December 1, 2008
?
I would suggset you correct your date picker to return a date string representation which is easy to parse:
@Test
public void formatDateFromPicker() throws ParseException {
// values from date picker
int day = 15;
int month = 0;
int year = 2009;
// build the easy to parse string from date picker: 15012009
String strDate = String.format("%02d%02d%04d",
day, (month+1 /* add 1 if months start at 0 */), year);
System.out.println(strDate);
// parse the date string from the date picker
Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);
// ouput January 15, 2009
System.out.println(new SimpleDateFormat("MMMM dd, yyyy").format(date));
}