tl;dr
YearMonth.from(
LocalDate.parse( "2016-03-20" )
)
.format(
DateTimeFormatter.ofPattern(
"MMM uuuu" ,
Locale.US
)
)
Mar 2016
java.time
Avoid the troublesome old date-time classes. Entirely supplanted by the java.time classes.
YearMonth
Java has a class to represent a year and month. Oddly named, YearMonth
.
LocalDate ld = LocalDate.parse( "2016-03-20" ) ;
YearMonth ym = YearMonth.from( ld ) ;
Define a formatting pattern for your desired output.
Specify a Locale
. A Locale
determines (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.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMM uuuu" , Locale.US ) ;
String output = ym.format( f ) ;
Mar 2016
You could use that formatter on the LocalDate
. But I assume you may have further need of the year-month as a concept, and so the YearMonth
class may be useful.
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.