getdate from datepicker android

Aymen Taarit picture Aymen Taarit · Dec 7, 2011 · Viewed 53k times · Source

I want to get date from datepicker widget in android I have tried with this

Date date1= (Date) new Date
   (dpBirthDate.getYear(), dpBirthDate.getMonth(), dpBirthDate.getDayOfMonth());
date1  = (Date) new SimpleDateFormat("yyyy-MM-dd").parse(date1.toString());

But I get a date like this mon 7 dec 2011 time ... and all I want to get is the yyyy-MM-dd format to store it in the database.

I tried also to concat the year-month-day like this but the problem is for example today 2011-12-7 the day should be 07 to be valid

Could you help me please.

Answer

Andrés Canavesi picture Andrés Canavesi · Jan 29, 2013

I use this:

    /**
 * 
 * @param datePicker
 * @return a java.util.Date
 */
public static java.util.Date getDateFromDatePicker(DatePicker datePicker){
    int day = datePicker.getDayOfMonth();
    int month = datePicker.getMonth();
    int year =  datePicker.getYear();

    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);

    return calendar.getTime();
}