如何解决月名称作为字符串
| 我正在尝试以字符串形式返回月份的名称,例如\“ May \”,\“ September \”,\“ November \”。 我试过了:int month = c.get(Calendar.MONTH);
但是,这将返回整数(分别为5、9、11)。如何获得月份名称?
解决方法
使用getDisplayName。
对于较早的API,请使用
String.format(Locale.US,\"%tB\",c);
, 用这个 :
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat(\"MMMM\");
String month_name = month_date.format(cal.getTime());
月名称将包含完整的月名称,如果要使用短月名称
这个
SimpleDateFormat month_date = new SimpleDateFormat(\"MMM\");
String month_name = month_date.format(cal.getTime());
, 要获取字符串变量中的月份,请使用以下代码
例如,9月:
M-> 9
MM-> 09
MMM-> 9月
MMMM-> 9月
String monthname=(String)android.text.format.DateFormat.format(\"MMMM\",new Date())
, \“ MMMM \”绝对不是正确的解决方案(即使它适用于多种语言),请使用\“ LLLL \”模式和SimpleDateFormat
作为独立月份名称的ICU兼容扩展名,对“ L”的支持已于2010年6月添加到Android平台。
即使用英语在\'MMMM \'和\'LLLL \'编码之间没有区别,您也应该考虑其他语言。
例如。如果您使用ѭ6if或一月\“ MMMM \”模式与俄语Locale
一起使用,这就是您得到的:
января(这对于完整的日期字符串是正确的:\“ 10января,2014 \”)
但是如果使用独立的月份名称,您将期望:
январь
正确的解决方案是:
SimpleDateFormat dateFormat = new SimpleDateFormat( \"LLLL\",Locale.getDefault() );
dateFormat.format( date );
如果您对所有翻译的来源感兴趣-这里是公历翻译的参考(其他日历链接在页面顶部)。
, 就这么简单
mCalendar = Calendar.getInstance();
String month = mCalendar.getDisplayName(Calendar.MONTH,Calendar.LONG,Locale.getDefault());
, 我保留此答案,这对其他情况很有用,但是@trutheality答案似乎是最简单直接的方法。
您可以使用DateFormatSymbols
DateFormatSymbols(Locale.FRENCH).getMonths()[month]; // FRENCH as an example
, 在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);
}
经过API 15-25测试
五月的输出是Май,但不是Мая
, 我建议使用Calendar
对象和Locale
,因为不同语言的月份名称不同:
// index can be 0 - 11
private String getMonthName(final int index,final Locale locale,final boolean shortName)
{
String format = \"%tB\";
if (shortName)
format = \"%tb\";
Calendar calendar = Calendar.getInstance(locale);
calendar.set(Calendar.MONTH,index);
calendar.set(Calendar.DAY_OF_MONTH,1);
return String.format(locale,format,calendar);
}
完整月份名称的示例:
System.out.println(getMonthName(0,Locale.US,false));
结果:January
短月份名称的示例:
System.out.println(getMonthName(0,true));
结果:Jan
, 获取日期和时间的示例方法
此格式\“ 2018 Nov 01 16:18:22 \”使用此格式
DateFormat dateFormat = new SimpleDateFormat(\"yyyy MMM dd HH:mm:ss\");
Date date = new Date();
dateFormat.format(date);
,俄语。
Month
.MAY
.getDisplayName(
TextStyle.FULL_STANDALONE,new Locale( \"ru\",\"RU\" )
)
май
美国的英语。
Month
.MAY
.getDisplayName(
TextStyle.FULL_STANDALONE,Locale.US
)
可以
看到此代码在IdeOne.com上实时运行。
ThreeTenABP和java.time
这是现代的答案。在2011年提出这个问题时,ѭ12和times23经常被设计为日期和时间,尽管它们的设计总是很差。那是8年前了,那些课程早已过时了。假设您尚未达到API级别26,我的建议是使用ThreeTenABP库,该库包含Android改编的java.time反向端口,即现代的Java日期和时间API。 java.time更好用。
根据您的确切需求和情况,有两种选择:
使用Month
及其getDisplayName
方法。
用a26ѭ。
使用月份
Locale desiredLanguage = Locale.ENGLISH;
Month m = Month.MAY;
String monthName = m.getDisplayName(TextStyle.FULL,desiredLanguage);
System.out.println(monthName);
该代码段的输出为:
可以
在某些语言中,使用TextStyle.FULL
还是TextStyle.FULL_STANDALONE
都会有所不同。您将不得不查看(也许与您的用户核对)两者中的哪一个适合您的上下文。
使用DateTimeFormatter
如果您有约会,但没有时间,我会建议您多付26英镑。例如:
DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern(\"MMMM\",desiredLanguage);
ZonedDateTime dateTime = ZonedDateTime.of(2019,5,31,23,49,51,ZoneId.of(\"America/Araguaina\"));
String monthName = dateTime.format(monthFormatter);
我正在展示使用ZonedDateTime
,它是旧Calendar
类的最接近替代品。上面的代码也适用于LocalDate
,a35ѭ,MonthDay
,OffsetDateTime
和YearMonth
。
如果您从尚未升级到java.time的旧版API中获得ѭ12怎么办?转换为ZonedDateTime
并按上述步骤进行:
Calendar c = getCalendarFromLegacyApi();
ZonedDateTime dateTime = DateTimeUtils.toZonedDateTime(c);
其余与以前相同。
问题:java.time是否不需要Android API级别26?
java.time在旧的和更新的Android设备上均可正常运行。它至少需要Java 6。
在Java 8和更高版本以及更新的Android设备(来自API级别26)中,内置了现代API。
在非Android Java 6和7中,获取ThreeTen Backport,即现代类的backport(JSR 310的ThreeTen;请参见底部的链接)。
在较旧的Android上,请使用Android版本的ThreeTen Backport。叫做ThreeTenABP。并确保您从带有子包的org.threeten.bp
中导入日期和时间类。
链接
Oracle教程:Date Time说明如何使用java.time。
Java Specification Request(JSR)310,其中首先描述了“ 43”。
ThreeTen Backport项目,,43到Java 6和7的反向移植(JSR-310的ThreeTen)。
ThreeTenABP,ThreeTen Backport的Android版
问题:如何在Android Project中使用ThreeTenABP,并有非常详尽的说明。
, 在Java中,很难获得一个独立的月份名称,很难做到“正确”。 (至少在撰写本文时。我当前正在使用Java 8)。
问题是,在某些语言(包括俄语和捷克语)中,月名称的独立版本与\“ formatting \\”版本不同。而且,似乎没有任何Java API会仅给您“最佳”字符串。到目前为止,此处发布的大多数答案仅提供格式化版本。下面粘贴的是一个有效的解决方案,用于获取单月名称的独立版本,或获取包含所有月份名称的数组。
我希望这可以节省一些时间!
/**
* getStandaloneMonthName,This returns a standalone month name for the specified month,in the
* specified locale. In some languages,including Russian and Czech,the standalone version of
* the month name is different from the version of the month name you would use as part of a
* full date. (Different from the formatting version).
*
* This tries to get the standalone version first. If no mapping is found for a standalone
* version (Presumably because the supplied language has no standalone version),then this will
* return the formatting version of the month name.
*/
private static String getStandaloneMonthName(Month month,Locale locale,boolean capitalize) {
// Attempt to get the standalone version of the month name.
String monthName = month.getDisplayName(TextStyle.FULL_STANDALONE,locale);
String monthNumber = \"\" + month.getValue();
// If no mapping was found,then get the formatting version of the month name.
if (monthName.equals(monthNumber)) {
DateFormatSymbols dateSymbols = DateFormatSymbols.getInstance(locale);
monthName = dateSymbols.getMonths()[month.getValue()];
}
// If needed,capitalize the month name.
if ((capitalize) && (monthName != null) && (monthName.length() > 0)) {
monthName = monthName.substring(0,1).toUpperCase(locale) + monthName.substring(1);
}
return monthName;
}
/**
* getStandaloneMonthNames,This returns an array with the standalone version of the full month
* names.
*/
private static String[] getStandaloneMonthNames(Locale locale,boolean capitalize) {
Month[] monthEnums = Month.values();
ArrayList<String> monthNamesArrayList = new ArrayList<>();
for (Month monthEnum : monthEnums) {
monthNamesArrayList.add(getStandaloneMonthName(monthEnum,locale,capitalize));
}
// Convert the arraylist to a string array,and return the array.
String[] monthNames = monthNamesArrayList.toArray(new String[]{});
return monthNames;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。