月名称作为字符串

如何解决月名称作为字符串

| 我正在尝试以字符串形式返回月份的名称,例如\“ 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-