36

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.

gideon
  • 19,329
  • 11
  • 72
  • 113
Aymen Taarit
  • 672
  • 1
  • 10
  • 22

4 Answers4

85

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();
}
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
Andrés Canavesi
  • 2,164
  • 20
  • 21
14

You should use SimpleDateFormat.format to convert that Date object to a String, like this

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString = sdf.format(date1);

I think you just had it backwards. You need to use format to go from Date to String, and parse to go from a String to a Date.

skynet
  • 9,898
  • 5
  • 43
  • 52
  • changed the code like this Date date1= (Date) new Date(dpBirthDate.getYear(), dpBirthDate.getMonth(), dpBirthDate.getDayOfMonth()); dates =new SimpleDateFormat("yyyy-MM-dd").format(date1); and what I got 3911-12-07 whats wrong – Aymen Taarit Dec 07 '11 at 01:08
  • Well then check what the year of the Date object is before you pass it in – skynet Dec 07 '11 at 01:12
  • how can I fix the Date format please help ! I always get the date strats with 3911 – Aymen Taarit Dec 08 '11 at 20:21
  • Then you are creating the Date object incorrectly. As I said, you should debug what the value actually is. If you look at the documentation of [`Date`](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html#Date%28int,%20int,%20int%29) you see that the year you pass in must be "the year minus 1900", so that is probably your error. Java's `Date` object has to be handled with care. – skynet Dec 08 '11 at 20:39
  • thank you now its clear I should do 3911 -1900 to get the proper year 2011 – Aymen Taarit Dec 08 '11 at 20:42
5

I know this is old but i struggled to find it. So for posterity

Scenario: A View needs the date in yyyy-mm-dd

    date_field = (EditText)findViewById(R.id.input_date);
        date_field.setFocusable(false); // disable editing of this field
        date_field.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                chooseDate();
            }
        });

 private void chooseDate() {
        final Calendar calendar = Calendar.getInstance();
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePicker =
            new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(final DatePicker view, final int year, final int month,
                                      final int dayOfMonth) {

                    @SuppressLint("SimpleDateFormat")
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    calendar.set(year, month, dayOfMonth);
                    String dateString = sdf.format(calendar.getTime());

                    date_field.setText(dateString); // set the date
                }
            }, year, month, day); // set date picker to current date

        datePicker.show();

        datePicker.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(final DialogInterface dialog) {
                dialog.dismiss();
            }
        });
    }

Hope this helps someone

blongho
  • 1,151
  • 9
  • 12
0

If your formatting was simple as numbers,

eg: dd/MM/yyyy

You can use the Kotlin way:

val dp = binding.datePicker
val date =  "${dp.dayOfMonth.let { if(it < 10) "0$it" else it }}/${dp.month.plus(1).let { if(it < 10) "0$it" else it }}/${dp.year}"
Yasser AKBBACH
  • 539
  • 5
  • 7