如何获取日期格式以大写月份和日期

发布于 2024-10-07 18:25:50 字数 689 浏览 7 评论 0原文

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

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

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

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

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

I have my strings like so in my 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花落人离去 2024-10-14 18:25:50

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

(这是为了防止您想要大写 - 即仅将第一个字母大写。否则您可以简单地使用 .toUpperCase()

更新:因为看起来这是 android ,您可以打开 WordUtils 的源代码并从那里复制实现,而不是获取整个库。

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

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

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

那些过往 2024-10-14 18:25:50

字符串的 toUpperCase()

String's toUpperCase() ?

筱武穆 2024-10-14 18:25:50

我像这样修复了它:

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

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));
離人涙 2024-10-14 18:25:50

有更精确的方法可以做到这一点。您可以使用 DateFormatSymbols 控制格式化程序的每个部分。

看看这个,真漂亮:

Scanner s = new Scanner(System.in);
SimpleDateFormat simpleDateFormatFrom = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat("MMM dd, yyyy");

DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
String[] months = simpleDateFormatTo.getDateFormatSymbols().getShortMonths();
for (int i = 0; i < months.length; i++) {
    months[i] = months[i].toUpperCase();
}
dateFormatSymbols.setShortMonths(months);
simpleDateFormatTo.setDateFormatSymbols(dateFormatSymbols);

Date date = simpleDateFormatFrom.parse(s.next());
System.out.println(simpleDateFormatTo.format(date));

There are more precise way of doing this. You can control each part of your formatter using DateFormatSymbols.

Look at that, this is beautiful:

Scanner s = new Scanner(System.in);
SimpleDateFormat simpleDateFormatFrom = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat("MMM dd, yyyy");

DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
String[] months = simpleDateFormatTo.getDateFormatSymbols().getShortMonths();
for (int i = 0; i < months.length; i++) {
    months[i] = months[i].toUpperCase();
}
dateFormatSymbols.setShortMonths(months);
simpleDateFormatTo.setDateFormatSymbols(dateFormatSymbols);

Date date = simpleDateFormatFrom.parse(s.next());
System.out.println(simpleDateFormatTo.format(date));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文