-3

Let's say I have an instance of a java.util.Date object.

Date date = new Date();

What is the idiomatic way to extract the year and the month? It seems that the methods getMonth and getYear have been deprecated. What should I use instead?

David Williams
  • 8,388
  • 23
  • 83
  • 171
  • Seems like it, thanks for the heads up. – David Williams Feb 20 '14 at 21:47
  • 3
    The javadoc explains why they've been deprecated, and tells what you should use instead. Read it. – JB Nizet Feb 20 '14 at 21:48
  • Wow, -2, touch crowd. I did, I just didn't find it immediately explanatory. `getMonth @Deprecated public int getMonth() Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH). Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January. Returns: the month represented by this date. See Also: Calendar` This should be something more like `calendarInstance.get(Calendar.MONTH)` – David Williams Feb 21 '14 at 00:29

2 Answers2

3

Date API in Java really is overcomplicated. You should use

 Calendar c = Calendar.getInstance();
 c.setTime(date);
 c.get(Calendar.MONTH);
 c.get(Calendar.YEAR);
renke
  • 1,180
  • 2
  • 12
  • 27
  • Thanks Joao, that was exactly the snippet I was missing in the docs. – David Williams Feb 21 '14 at 00:29
  • 1
    Actually, both Date *and* Calendar are overcomplicated. Use Joda-Time or java.time.* in Java 8. – Basil Bourque Feb 21 '14 at 04:43
  • This as it turns out is sufficient for my needs because I am only manipulating dates previously generated and persisted to a database without timezone data appended. For all things that need timezone aware or consistent behavior I will surely use joda, or upgrade to java 8. – David Williams Feb 21 '14 at 18:47
2

The java.util.Date & Calendar classes bundled with Java are notoriously troublesome. Avoid them.

Both the question and other answer ignore the critical issue of time zone. If you neglect to specify a time zone, you will get the JVM's default. That means the results of your code vary when run on different computers or changed JVM settings, probably not what you want.

Joda-Time

This third-party open-source library, Joda-Time, is a popular replacement.

Specify a time zone rather than rely on default.

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
java.util.Locale locale = java.util.Locale.FRANCE;

DateTime now = new DateTime( timeZone );
int dayOfMonth = now.getDayOfMonth(); 
int monthOfYear = now.getMonthOfYear();  // 1-based counting, January = 1, unlike java.util.Calendar.
String nowAsString = DateTimeFormat.forStyle( "F-" ).withLocale( locale ).print( now );

Dump to console…

System.out.println( "now: " + now );
System.out.println( "dayOfMonth: " + dayOfMonth );
System.out.println( "monthOfYear: " + monthOfYear );
System.out.println( "nowAsString: " + nowAsString );

When run…

now: 2014-02-21T05:51:18.688+01:00
dayOfMonth: 21
monthOfYear: 2
nowAsString: vendredi 21 février 2014

java.time.*

Java 8 brings a new java.time.8 package to supplant the old j.u.Date/Calendar classes. This new package is inspired by Joda-Time (but re-architected) and defined by JSR 310.

ZoneId zoneId = ZoneId.of( "Europe/Paris" );
ZonedDateTime now = ZonedDateTime.now( zoneId );

int dayOfMonth = now.getDayOfMonth();
int monthOfYear = now.getMonthValue();  // 1-based counting, January = 1, unlike java.util.Calendar.
String nowAsString = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( Locale.FRANCE ).format( now );

Dump to console…

System.out.println( "now: " + now );
System.out.println( "dayOfMonth: " + dayOfMonth );
System.out.println( "monthOfYear: " + monthOfYear );
System.out.println( "nowAsString: " + nowAsString );

When run…

now: 2014-02-21T06:05:48.833+01:00[Europe/Paris]
dayOfMonth: 21
monthOfYear: 2
nowAsString: vendredi 21 février 2014
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Basil, great answer, thank you for putting that together. I am familiar with joda time and time zone usage. I thought I didnt have joda time as part of the third party libs on a project I am working on, but it turns out I do as part of the dependencies to gapi. Cheers – David Williams Feb 21 '14 at 18:43