tl;dr
MonthDay.from(
LocalDate.now( ZoneId.of( "America/Montreal" ) )
).format( DateTimeFormatter.ofPattern( "MM/dd" ) )
01/24
MonthDay
There's a class for that: MonthDay
.
The MonthDay
represents the combination of a month and day-of-month.
MonthDay md = MonthDay.of( Month.JANUARY , 24 );
Avoid legacy date-time classes.
Avoid the troublesome old date-time classes such as SimpleDateFormat
, now legacy, supplanted by the java.time classes.
The MonthDay
class is part of the java.time framework built into Java 8 and later. Much of java.time is back-ported to Java 6 & Java 7 (see below).
ISO 8601
md.toString(): --01-24
The toString
method generates a String in standard ISO 8601 format. The MonthDay
class can also parse such strings.
Current date
You want the month-day of the current date. For the current date we will use the LocalDate
class. The LocalDate
class represents a date-only value without time-of-day and without time zone.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
Be clear that Locale
has nothing to do with time zones. A Locale
only applies to the format of text when generating strings, but is separate and distinct from time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Now extract a MonthDay
object.
MonthDay md = MonthDay.from( today );
today.toString(): 2017-01-25
md.toString(): --01-25
Other formats
You can generate strings in other formats.
The java.time classes can automatically localize some date-time values by Locale
. Unfortunately, this does not apply to MonthDay
. For MonthDay
you must explicitly specify the desired format in a DateTimeFormatter
object when you want something other than the standard ISO 8601 format. By the way, I encourage you to stick with the standard format whenever possible, especially for exchanging data. You might even consider training your users to accept this format. The standard format is unambiguous, whereas your UK-US formats (dd/MM, MM/dd) can be entirely ambiguous such as 01/02 being either January 2nd or February 1st.
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, and such. Generally, I advise always specifying a Locale
for your DateTimeFormatter
rather than relying implicitly on the JVM’s current default Locale
. But I do not see any way the Locale
would affect the output of this particular format. So I omit the Locale
in this example.
DateTimeFormatter fUS = DateTimeFormatter.ofPattern( "MM/dd" );
DateTimeFormatter fUK = DateTimeFormatter.ofPattern( "dd/MM" );
String output = md.format( fUS );
Live code
See this example code run live at IdeOne.com.
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.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.