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

4

11

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.

Petrick answered 12/4, 2017 at 20:58 Comment(0)
H
25

The string you are looking for is MMMM.

Source: DateTimeFormatter Javadoc

Herophilus answered 12/4, 2017 at 21:0 Comment(3)
Thanks. How about full day?Petrick
Oh, and full day is EEEE! Got it from your javadoc. Thanks.Petrick
Ah, I thought you meant day of month, not day of week.Herophilus
T
20

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?

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.

Tilford answered 13/4, 2017 at 4:33 Comment(1)
Some locale constants are not available. You can replace Locale.UK constant with new Locale("ru") then.Eadith
E
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
Extortioner answered 4/6, 2017 at 5:37 Comment(0)
T
1
import java.time.LocalDate;

Just use the getDayOfWeek()

LocalDate.of(year, month, day).getDayOfWeek().name()

You can use it as

public static String dayName(int month, int day, int year) {

    return LocalDate.of(year, month, day).getDayOfWeek().name();

}
Towhee answered 16/6, 2020 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.