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

≯℡__Kan透↙ 提交于 2019-12-22 07:04:32

问题


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.


回答1:


The string you are looking for is MMMM.

Source: DateTimeFormatter Javadoc




回答2:


tl;dr

Use automatic localization. No need to specify formatting pattern.

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

Monday, 23 January 2017

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

janvier

Month

Taking your title literally, I would use the handy Month enum.

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:

  • TextStyle to determine how long or abbreviated should the string be.
  • Locale to 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.

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 DateTimeFormatter do the work. Here we use FormatStyle rather than 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


About java.time

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

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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

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

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project 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.




回答3:


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


来源:https://stackoverflow.com/questions/43379533/how-to-get-full-name-of-month-from-date-in-java-8-while-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!