Getting a month from a date

T

Tim Slattery

I need to extract the month from a Date object as an integer, where
month would range from 1 (for January) to 12 (for December). In the
documentation for the Data object, I see a deprecated getMonth().
Instead of getMonth, we're supposed to create a Calendar object and
use the Calendar.get(Calendar.MONTH) method.

But that method returns one of the Calendar values JANUARY,
FEBRUARY....DECEMBER. So, to get my integer I have to make a long
switch statement to figure out which of these values was returned. I
can't find any discussion of what the month values actually are.

Is there a way to shortcut this rigamarole? It seems that what should
take one line of code is taking about 40 instead.
 
C

Carl

Tim said:
I need to extract the month from a Date object as an integer, where
month would range from 1 (for January) to 12 (for December). In the
documentation for the Data object, I see a deprecated getMonth().
Instead of getMonth, we're supposed to create a Calendar object and
use the Calendar.get(Calendar.MONTH) method.

But that method returns one of the Calendar values JANUARY,
FEBRUARY....DECEMBER. So, to get my integer I have to make a long
switch statement to figure out which of these values was returned. I
can't find any discussion of what the month values actually are.

Is there a way to shortcut this rigamarole? It seems that what should
take one line of code is taking about 40 instead.

Not Quite, the Calendar.get() method returns an integer:
public int get(int field)

In the case of Calendar.MONTH, remember that the month count is zero
based, so:

GregorianCalendar gc = new GregorianCalendar();
System.out.println("Month: " + gc.get(GregorianCalendar.MONTH));

Prints 10 for November if run today. This is clearly documented here:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#MONTH
 
S

Sudsy

Tim said:
I need to extract the month from a Date object as an integer, where
month would range from 1 (for January) to 12 (for December). In the
documentation for the Data object, I see a deprecated getMonth().
Instead of getMonth, we're supposed to create a Calendar object and
use the Calendar.get(Calendar.MONTH) method.

But that method returns one of the Calendar values JANUARY,
FEBRUARY....DECEMBER. So, to get my integer I have to make a long
switch statement to figure out which of these values was returned. I
can't find any discussion of what the month values actually are.

Is there a way to shortcut this rigamarole? It seems that what should
take one line of code is taking about 40 instead.

This may be an obtuse approach, but it's tested and works:

String currentMonth = new String[] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } [ new
GregorianCalendar().get( Calendar.MONTH ) ];

There's your 1 line of code...adjust the String array contents
to your liking.
 
J

Jacob

Tim said:
I need to extract the month from a Date object as an integer, where
month would range from 1 (for January) to 12 (for December). In the
documentation for the Data object, I see a deprecated getMonth().
Instead of getMonth, we're supposed to create a Calendar object and
use the Calendar.get(Calendar.MONTH) method.

The pragmatic solution is to use Calendar.get (Calendar.MONTH) + 1

This is conceptually wrong however (as you have realized), and even
if it produce the correct answer till the end of time, I'll consider
it a bug.

Making the months an enumeration was just one of the blunders SUN has
made with the date/calendar classes. How many pending bugs are there
out there because of the approach above but where the "+ 1" part is
missing?

For a more convenient date API you might look at the "day" module in
http://geosoft.no/software/index.html. The Day class includes (among
other useful features) a getMonth() *and* a get getMonthNo() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top