TL; dr
LocalDate.parse( "2011-12-15" ) // Date-only, without time-of-day, without time zone. .format( // Generate 'String' representing value of this 'LocalDate'. DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG ) // How long or abbreviated? .withLocale( // Locale used in localizing the string being generated. new Locale( "en" , "IN" ) // English language, India cultural norms. ) // Returns a 'DateTimeFormatter' object. ) // Returns a 'String' object.
December 15, 2011
java.time
While the accepted answer is correct (capital MM
for the month), there is now a better approach. The problematic old date and time classes are now inherited; they are replaced by the java.time classes.
Your input string is in standard ISO 8601 format . Therefore, you do not need to specify a formatting template for parsing.
LocalDate ld = LocalDate.parse( "2011-12-15" ); // Parses standard ISO 8601 format by default. Locale l = new Locale( "en" , "IN" ) ; // English in India. DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG ) .withLocale( l ); String output = ld.format( f );
Dump to the console.
System.out.println( "ld.toString(): " + ld ); System.out.println( "output: " + output );
ld.toString (): 2011-12-15
day off: December 15, 2011
See live code at IdeOne.com .
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete date and time classes, such as java.util.Date
, Calendar
, and SimpleDateFormat
.
The Joda-Time project, currently in maintenance mode , recommends switching to the java.time classes.
To learn more, see the Oracle Tutorial . And a search for many examples and explanations. JSR 310 specification .
You can exchange java.time objects directly with your database. Use a JDBC driver that conforms to JDBC 4.2 or later. No strings needed, no java.sql.*
Needed.
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes such as Interval
, YearWeek
, YearQuarter
and others .