java 格式化时如何从Java 8中的日期获取月份的全名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43379533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:23:16  来源:igfitidea点击:

How to get full name of month from date in Java 8 while formatting

javalocalizationdate-formattingjava-time

提问by Apurva Singh

Using new Java 8 java.time API, I need to convert LocalDate and get full name of month and day. Like March (not Mar), and Monday (not Mon). Friday the 13th March should be formatted like Friday, 13 March.. not Fri, 13 Mar.

使用新的 Java 8 java.time API,我需要转换 LocalDate 并获取月和日的全名。像三月(不是三月)和星期一(不是星期一)。3 月 13 日星期五的格式应类似于 3 月 13 日星期五……而不是 3 月 13 日星期五。

回答by Joe C

The string you are looking for is MMMM.

您要查找的字符串是MMMM.

Source: DateTimeFormatter Javadoc

来源:DateTimeFormatter Javadoc

回答by Basil Bourque

tl;dr

tl;博士

Use automatic localization. No need to specify formatting pattern.

使用自动本地化。无需指定格式模式。

localDate.format( 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
                     .withLocale( Locale.UK )
)

Monday, 23 January 2017

2017 年 1 月 23 日,星期一

LocalDate
.of( 2017 , Month.JANUARY , 23 )
.getMonth()
.getDisplayName(
    TextStyle.FULL , 
    Locale.CANADA_FRENCH 
)

janvier

詹维尔

Month

Taking your title literally, I would use the handy Monthenum.

从字面上看你的标题,我会使用方便的Month枚举。

LocalDate ld = LocalDate.of( 2017 , Month.JANUARY , 23 );
Month month = ld.getMonth() ;  // Returns a `Month` object, whereas `getMonthValue` returns an integer month number (1-12).

Let java.time do the work of automatically localizing. To localize, specify:

让 java.time 做自动本地化的工作。要本地化,请指定:

  • TextStyleto determine how long or abbreviated should the string be.
  • Localeto determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
  • TextStyle确定字符串的长度或缩写。
  • Locale确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。

For example:

例如:

String output = month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Or Locale.US, Locale.KOREA, etc.

janvier

詹维尔

Date

日期

If you want the entire date localized, let DateTimeFormatterdo the work. Here we use FormatStylerather than TextStyle.

如果您想要本地化整个日期,让我们DateTimeFormatter来做这项工作。这里我们使用FormatStyle而不是TextStyle

Example:

例子:

Locale l = Locale.CANADA_FRENCH ;  // Or Locale.US, Locale.KOREA, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )
                                       .withLocale( l ) ;
String output = ld.format( f );

dimanche 23 janvier 2107

2107 年 1 月 23 日



About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by Dennis

Yes. Now it can be done.

是的。现在可以做到了。

LocalDate dd = new LocalDate();  //pass in a date value or params(yyyy,mm)

String ss = dd.monthOfYear.getAsText(); // will give the full name of the month
String sh = dd.monthOfYear.getAsShortText(); // shortform