The name of the month appears with the number M + in the datePicker dialog in android

I am trying to display a date in a datePicker dialog in the format Sep|29|2016 . You can see in the following image.

Expected Result

enter image description here

Unfortunately, most of the time I get 28|M09|2016 rarely, I get the expected result. you can see in the following image

current output

enter image description here

I tried with the following code

 public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US)); calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US); int yy = calendar.get(Calendar.YEAR); int mm = calendar.get(Calendar.MONTH); int dd = calendar.get(Calendar.DAY_OF_MONTH); System.out.println("yearr==="+yy); System.out.println("monthh==="+mm); System.out.println("dayy==="+dd); DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(), this,yy,mm,dd); return datepickerdialog; } 

Thanks pending.

+7
android android-datepicker datepickerdialog
source share
5 answers

Are you expecting this?

  Button selectDate = (Button) findViewById(R.id.idButton); selectDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Calendar currentDate = Calendar.getInstance(Locale.ENGLISH); int mYear = currentDate.get(Calendar.YEAR); int mMonth = currentDate.get(Calendar.MONTH); int mDay = currentDate.get(Calendar.DAY_OF_MONTH); DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int selectedyear, int selectedmonth, int selectedday) { GregorianCalendar gc = new GregorianCalendar(); gc.setFirstDayOfWeek(Calendar.MONDAY); gc.set(Calendar.MONTH, view.getMonth()); gc.set(Calendar.DAY_OF_MONTH, view.getDayOfMonth()); gc.set(Calendar.YEAR, view.getYear()); DecimalFormat mFormat = new DecimalFormat("00"); String selectedDate = String.format("%s/%S/%s", gc.get(Calendar.YEAR), mFormat.format(gc.get(Calendar.MONTH) + 1), mFormat.format(gc.get(Calendar.DATE))); } }, mYear, mMonth, mDay); mDatePicker.setTitle("Select Date"); mDatePicker.show(); } }); 

output

+3
source share
 Calendar myCalendar = Calendar.getInstance(); DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub myCalendar.set(Calendar.YEAR, year); myCalendar.set(Calendar.MONTH, monthOfYear); myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateLabel(); } }; edittext.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new DatePickerDialog(classname.this, date, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); } }); private void updateLabel() { String myFormat = "MM/dd/yy"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); edittext.setText(sdf.format(myCalendar.getTime())); } 
+2
source share

Perhaps the problem is with Locale .

Set Locale current to dialog . Remove Locale.US where the problem occurs.

 Locale locale = getResources().getConfiguration().locale; Locale.setDefault(locale); 

After that set Locale to dialog .

Then add dialog material.

Android N (Api 24 level update) (no warnings):

 @TargetApi(Build.VERSION_CODES.N) public Locale getCurrentLocale(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ return getResources().getConfiguration().getLocales().get(0); } else{ //noinspection deprecation return getResources().getConfiguration().locale; } } 
+1
source share

There may be a version of the OS on the tablet you are using. This is not a language because you use this to get a string, not a calendar instance. Use AppCompatDialogFragment (import android.support.v7.app.AppCompatDialogFragment)

 public class DatePickerFragment extends AppCompatDialogFragment implements DatePickerDialog.OnDateSetListener { private static final String TAG = "DatePickerFormat"; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Log.d(TAG, String.format("onDateSet: %d %d %d", year, month, dayOfMonth)); } } 
+1
source share

Have you changed your local configuration? It seems that datepickerdialog uses this format for standard (English) language when updating the locale configuration.

Instead of this

 Locale locale = new Locale(countryCode); Locale.setDefault(locale); 

Do this for countries with the language "en"

 Locale locale = new Locale(language, countryCode); Locale.setDefault(locale); 

Example

 Locale locale = new Locale("en", "GB"); 
0
source share

All Articles