Home » Android » java – getdate from datepicker android

java – getdate from datepicker android

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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();
}

Answer:

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.

Answer:

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