为什么DatePickerDialog标题显示的月份名称错误?

时间:2019-02-07 10:37:16

标签: android

我的问题是DatePickerDialog没有播下完整的月份名称。请给我我的问题的答案。

try {
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog datePickerDialog = new DatePickerDialog(mContext,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    strStartDate = Utils.getFormattedDate(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Constants.YYYY_MM_DD, "dd-M-yyyy");
                    strStartDateDB = Utils.getFormattedDate(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Constants.MMM_YYYY, "dd-M-yyyy");
                    Log.e("Date_Api", "" + strStartDate + "-" + strStartDateDB);
                    mBinding.tvStartDate.setText(Utils.getFormattedDate(strStartDate, Constants.DD_MM_YYYY, Constants.YYYY_MM_DD));
                    mBinding.tvStartDate.setTextColor(ContextCompat.getColor(mContext, R.color.black_1));
                }
            }, mYear, mMonth, mDay);

    if (!strStartDate.isEmpty()) {
        c.set(mYear, mMonth, mDay);
    }
    if (strEndDate.isEmpty()) {
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
    } else {
        c.set(mEndYear, mEndMonth, mEndDay);
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
        datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
    }
    datePickerDialog.show();
} catch (Exception e) {
    AppLog.showLogE("Calendar_exception" + e.getMessage());
}

现在的结果是

a busy cat

我希望结果像“ 2月11日,星期四和2019年2月”

4 个答案:

答案 0 :(得分:1)

将这个try块放在顶部

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

答案 1 :(得分:0)

为此找到了最简单的解决方案(实际上不是解决方案,它是导致此类问题的错误)。 您的 DatePickerDialog 没有获得默认语言环境,因此它显示了一些奇怪的月份选择标题。只需为英语或您要为应用程序设置的任何其他语言设置默认语言环境即可。

  //call this method in every acitivity before 
//  setContentView(R.layout.activity_main);
 setLanguage(this, "en);
// (change "en" as per your language)




   public static void setLanguage(Context context, String language) {
            Resources res = context.getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            android.content.res.Configuration conf = res.getConfiguration();
            conf.setLocale(new Locale(language, "in")); // API 17+ only.
            res.updateConfiguration(conf, dm);
            context.getApplicationContext().getResources().updateConfiguration(conf, 
            context.getResources().getDisplayMetrics());
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            context.getResources().updateConfiguration(config,                
            context.getResources().getDisplayMetrics());*/
        }

答案 2 :(得分:-1)

给出代码段,记录并检查以下代码行:

  strStartDate = Utils.getFormattedDate(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Constants.YYYY_MM_DD, "dd-M-yyyy");
                strStartDateDB = Utils.getFormattedDate(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Constants.MMM_YYYY, "dd-M-yyyy");
                Log.e("Date_Api", "" + strStartDate + "-" + strStartDateDB);
                mBinding.tvStartDate.setText(Utils.getFormattedDate(strStartDate, Constants.DD_MM_YYYY, Constants.YYYY_MM_DD));
                mBinding.tvStartDate.setTextColor(ContextCompat.getColor(mContext, R.color.black_1));

答案 3 :(得分:-1)

这是用于尝试此朋友的代码

    DatePickerDialog dialog = new DatePickerDialog(getActivity(), R.style.DateDialogTheme, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, 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);

            Calendar calendar = Calendar.getInstance();
            calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(),
                    0, 0, 0);
            long startTime = calendar.getTimeInMillis();

            if (startTime > System.currentTimeMillis()) {
                toast(getResources().getString(R.string.select_valid_date));
            } else {
                String myFormat = "yyyy-MM-dd";
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                getDataByDate(sdf.format(myCalendar.getTime()));
                getParentActivity().setHomeTabIcon(true);
            }
        }
    }, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
//    dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
    dialog.show();