Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 474
Apurva Singh Avatar asked Apr 12 '17 20:04

Apurva Singh


2 Answers

The string you are looking for is MMMM.

Source: DateTimeFormatter Javadoc

like image 103
Joe C Avatar answered Nov 17 '22 11:11

Joe C


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.

like image 45
Basil Bourque Avatar answered Nov 17 '22 11:11

Basil Bourque