如何获取日期格式以大写月份和日期
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
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.字符串的 toUpperCase()
?String's toUpperCase()
?我像这样修复了它:
I fixed it like so:
有更精确的方法可以做到这一点。您可以使用 DateFormatSymbols 控制格式化程序的每个部分。
看看这个,真漂亮:
There are more precise way of doing this. You can control each part of your formatter using DateFormatSymbols.
Look at that, this is beautiful: