tl;dr
Avoid the troublesome legacy date-time classes. Use only java.time classes.
LocalDate.now() // Determine the current date as seen in the wall-clock time in use by JVM’s current default time zone. Better to specify explicitly your desired/expected time zone.
.getMonth() // Get the `Month` enum object representing the month of that `LocalDate` object’s date value.
.getDisplayName( // Let the `Month` enum object automatically localize to generate the string of the name of the month.
TextStyle.SHORT , // Specify how long or abbreviate you want the text of the name of the month.
Locale.US // Specify a `Locale` to determine the human language and cultural norms to be used in localizing the name of the month.
) // Return a String.
Jan
java.time
The modern approach uses java.time classes.
Getting the current month means determining the current date. And getting the current date requires a time zone, as for any given moment the date varies around the globe by zone.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate ld = LocalDate.now( z ) ;
Month m = ld.getMonth();
Let the Month
enum automatically localize the name of the month. Control abbreviation by passing a TextStyle
.
String output = m.getDisplayName( TextStyle.SHORT , Locale.US );
Apr
Date with time
If your goal is to combine a time-of-day with your date, use ZonedDateTime
class. You must specify a time zone to determine the first moment of the day. The day does not start at 00:00:00 on some dates in some zones.
ZonedDateTime zdt = ld.atStartOfDay( z ) ; // Determine the first moment of the day.
Do not use java.sql.Timestamp
for the purpose of generating a String. Use the DateTimeFormatter
class, documented in many other Answers on Stack Overflow.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MMM-dd HH:mm:ss" , Locale.US ) ;
String output = zdt.format( f ) ;
Or just call ZonedDateTime::toString
to generate a String in standard ISO 8601 format, wisely extended to append the name of the zone in square brackets.
String output = zdt.toString() ;
Indeed, do not use Timestamp
at all. That class is intended for use with databases, and is now replaced by Instant
as of JDBC 4.2 and later.
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.