Search code examples
androidsamsung-galaxy

Android version 9 DatePickerDialog dateset issue


My Android native application was working fine on older versions before 9. But, now on 9 on selecting date with SimpleDateFormat("MMM dd, yyyy"); date is selected as Jan. 07 , 2020 I don't understand from where a . is added after Jan which results in following error from service

java.text.ParseException: Unparseable date: "Jan. 07, 2020"

My android code

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            mEditTextDate.setText(getDateFormat(context).format(
                    cal.getTime()));
        }
    };

public DateFormat getDateFormat(Context context) {
        if (dateF == null)
            dateF = new SimpleDateFormat("MMM dd, yyyy");
        return dateF;
    }

I am testing on Samsung Galaxy Tablet S6. I also tried changing Settings-->GeneralManagement --> Date and Time to Automatic date and time. But same code is working on all other tablets with MMM dd, yyyy format


Solution

  • You should use Locale.ENGLISH to get your desired date. Try below:

    dateF = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);