java 如何让日期格式大写月份和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4416767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:16:46  来源:igfitidea点击:

how to get dateformat to capitalize month and day

javadatecapitalize

提问by tristan202

I have my strings like so in my strings.xml:

我的strings.xml中有这样的字符串:

<string name="day_format">EEEE</string>
<string name="date_format">dd. MMMM</string>
<string name="date_format_us">MMMM dd</string>

And I use them like this in the code:

我在代码中像这样使用它们:

    private void reinit() {
    mDayFormat = getString(R.string.day_format);
    if (!DateFormat.is24HourFormat(this))
    {
    mDateFormat = getString(R.string.date_format_us);
    }
    else {
        mDateFormat = getString(R.string.date_format);
    }
    mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS;
    mCalendar = Calendar.getInstance();
}

But it displays the day and the month in lowercase, and I want it to capitalize both. How can I achieve that?

但它以小写形式显示日期和月份,我希望它同时大写。我怎样才能做到这一点?

采纳答案by tristan202

I fixed it like so:

我是这样修复的:

        final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
    final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
    String time = (String) DateFormat.format(mTimeFormat, mCalendar);
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.clock2by2);
    String days = new String(day.toString().substring(0,1).toUpperCase() + day.toString().substring(1));
    String dates = new String(date.toString().substring(0,1).toUpperCase() + date.toString().substring(1));

    views.setTextViewText(R.id.Day, days);
    views.setTextViewText(R.id.Date, dates);
    views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));

回答by Bozho

You can use WordUtils.capitalize(..)from commons-lang(on the result string)

您可以使用WordUtils.capitalize(..)from commons-lang(在结果字符串上)

(this is in case you want to capitalize - i.e. uppercase only the first letter. Otherwise you can simpyl use .toUpperCase())

(这是在你想大写的情况下 - 即只大写第一个字母。否则你可以简单地使用.toUpperCase()

Update: Since it appears this is android, you can open the sources of WordUtilsand copy the implementation from there, rather than getting the whole library.

更新:由于这似乎是 android,您可以打开源WordUtils并从那里复制实现,而不是获取整个库。

回答by madhurtanwani

String's toUpperCase()?

String's toUpperCase()?