Aymen Taarit
Aymen Taarit

Reputation: 672

getdate from datepicker android

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.

Upvotes: 36

Views: 61819

Answers (4)

Yasser AKBBACH
Yasser AKBBACH

Reputation: 1028

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}"

Upvotes: 0

blongho
blongho

Reputation: 1231

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

Upvotes: 5

Andr&#233;s Canavesi
Andr&#233;s Canavesi

Reputation: 2174

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

Upvotes: 85

skynet
skynet

Reputation: 9908

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.

Upvotes: 14

Related Questions