在android的datePicker对话框中,月份名称与M +数字一起出现

我正在尝试以像Sep|29|2016这样的格式在datePicker对话框中显示date。 你可以看到下面的图像。

预期产出

在这里输入图像说明

不幸的是,大多数时候我正在获得28|M09|2016很less得到预期的产出。 你可以看到下面的图像

电stream输出

在这里输入图像说明

我已经尝试了下面的代码

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

感谢预期。

Solutions Collecting From Web of "在android的datePicker对话框中,月份名称与M +数字一起出现"

你在期待吗?

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

产量

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

可能是由于Locale引起的问题。

dialog前设置当前的locale 删除Locale.US这是问题发生的地方。

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

将此localedialog

然后把你的dialog东西。

Android N (Api级别24)更新(无警告):

 @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; } } 

可能是您使用的平板电脑的操作系统版本。 这不是地区,因为你使用它来获取一个string,而不是日历实例。 使用AppCompatDialogFragment(导入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)); } } 

你有没有改变你的语言环境configuration? 看起来datepickerdialog在更新语言环境configuration时使用默认(英文)语言环境的格式。

而不是这个

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

为有“en”语言的国家做这个

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

样品

 Locale locale = new Locale("en", "GB");