我试图将月份的名称作为字符串返回,例如"May","September","November".
我试过了:
int month = c.get(Calendar.MONTH);
Run Code Online (Sandbox Code Playgroud)
但是,这会返回整数(分别为5,9,11).我怎样才能获得月份名称?
Joh*_*ohn 147
用这个 :
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
String month_name = month_date.format(cal.getTime());
Run Code Online (Sandbox Code Playgroud)
月份名称将包含完整的月份名称,如果您想要短月份名称使用此名称
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
String month_name = month_date.format(cal.getTime());
Run Code Online (Sandbox Code Playgroud)
Aru*_*ney 103
要获得字符串变量中的月份,请使用以下代码
例如9月:
M - > 9
MM - > 09
MMM - >九月
MMMM - >九月
String monthname=(String)android.text.format.DateFormat.format("MMMM", new Date())
Run Code Online (Sandbox Code Playgroud)
tru*_*ity 83
对于早期API的使用 String.format(Locale.US,"%tB",c);
art*_*nig 39
"MMMM"绝对不是正确的解决方案(即使它适用于多种语言),使用"LLLL"模式 SimpleDateFormat
2010年6月,Android平台增加了对"L"作为独立月份名称的ICU兼容扩展的支持.
即使用英语,'MMMM'和'LLLL'的编码也没有区别,你也应该考虑其他语言.
例如,如果你使用Calendar.getDisplayName俄罗斯的1月份的"MMMM"模式,这就是你得到的Locale:
января(对于完整的日期字符串是正确的:" 10января,2014 ")
但如果是单独的月份名称,您会期望:
январь
正确的解决方案是:
SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
dateFormat.format( date );
Run Code Online (Sandbox Code Playgroud)
如果您对所有翻译的来源感兴趣 - 这里是格里高利历日历翻译的参考(在页面顶部链接的其他日历).
san*_*one 15
就这么简单
mCalendar = Calendar.getInstance();
String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)
我保留这个对其他案例有用的答案,但@trutheality答案似乎是最简单直接的方法.
您可以使用DateFormatSymbols
DateFormatSymbols(Locale.FRENCH).getMonths()[month]; // FRENCH as an example
Run Code Online (Sandbox Code Playgroud)
俄语。
\n\nMonth\n.MAY\n.getDisplayName(\n TextStyle.FULL_STANDALONE ,\n new Locale( "ru" , "RU" )\n)\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\xd0\xbc\xd0\xb0\xd0\xb9
\n
在美国说英语。
\n\nMonth\n.MAY\n.getDisplayName(\n TextStyle.FULL_STANDALONE ,\n Locale.US\n)\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n\n\n可能
\n
这里\xe2\x80\x99是现代答案。当这个问题在 2011 年被问到时,Calendar通常GregorianCalendar用于日期和时间,尽管它们总是设计得很糟糕。那 \xe2\x80\x99 已经是 8 年前的事了,那些类早已过时了。假设您尚未达到 API 级别 26,我的建议是使用 ThreeTenABP 库,其中包含适用于 Android 的 java.time(现代 Java 日期和时间 API)的向后移植。java.time 更适合使用。
根据您的具体需求和情况,有两种选择:
\n\nMonth及其getDisplayName方法。DateTimeFormatter。 Locale desiredLanguage = Locale.ENGLISH;\n Month m = Month.MAY;\n String monthName = m.getDisplayName(TextStyle.FULL, desiredLanguage);\n System.out.println(monthName);\nRun Code Online (Sandbox Code Playgroud)\n\n该片段的输出是:
\n\n\n\n\n可能
\n
在某些语言中,是否使用TextStyle.FULL或都会有所不同TextStyle.FULL_STANDALONE。您必须查看,也许与您的用户核实,这两者中哪一个适合您的环境。
如果你\xe2\x80\x99已经得到了一个有或没有一天中时间的约会,我发现一个DateTimeFormatter更实用的。例如:
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", desiredLanguage);\n\n ZonedDateTime dateTime = ZonedDateTime.of(2019, 5, 31, 23, 49, 51, 0, ZoneId.of("America/Araguaina"));\n String monthName = dateTime.format(monthFormatter);\nRun Code Online (Sandbox Code Playgroud)\n\n我展示了 a 的使用ZonedDateTime,它是旧Calendar类最接近的替代品。上面的代码也适用于 a LocalDate、 a LocalDateTime、MonthDay和OffsetDateTimea YearMonth。
Calendar如果您从尚未升级到 java.time 的旧 API 中获取 a 该怎么办?转换为 aZonedDateTime并按上述方式进行:
Calendar c = getCalendarFromLegacyApi();\n ZonedDateTime dateTime = DateTimeUtils.toZonedDateTime(c);\nRun Code Online (Sandbox Code Playgroud)\n\n其余的和以前一样。
\n\njava.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少Java 6。
\n\norg.threeten.bp子包中导入日期和时间类。java.time。java.time到 Java 6 和 7(ThreeTen for JSR-310)。Android上获得乌克兰,俄语,捷克语等语言的格式正确的stanalone月名称的唯一方法
private String getMonthName(Calendar calendar, boolean short) {
int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NO_YEAR;
if (short) {
flags |= DateUtils.FORMAT_ABBREV_MONTH;
}
return DateUtils.formatDateTime(getContext(), calendar.getTimeInMillis(), flags);
}
Run Code Online (Sandbox Code Playgroud)
经过API 15-25测试
五月的输出是??? 但不是?
| 归档时间: |
|
| 查看次数: |
108855 次 |
| 最近记录: |