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.
The string you are looking for is MMMM
.
Source: DateTimeFormatter Javadoc
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
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With