Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting a month from a date

Reply
Thread Tools

Getting a month from a date

 
 
Tim Slattery
Guest
Posts: n/a
 
      11-08-2004
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.

--
Tim Slattery

 
Reply With Quote
 
 
 
 
Carl
Guest
Posts: n/a
 
      11-08-2004
Tim Slattery wrote:
> 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.
>
> --
> Tim Slattery
>


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/.../Calendar.html
http://java.sun.com/j2se/1.4.2/docs/...dar.html#MONTH

 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      11-09-2004
Tim Slattery wrote:
> 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.
>
> --
> Tim Slattery
>


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.

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      11-09-2004
Tim Slattery wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cal control - pre selecting a date from last month - and the calendar still shows this month. Mufasa ASP .Net 0 09-05-2008 06:51 PM
verifying a date is valid based on integer values for year, month and day of month lbrtchx@hotmail.com Java 1 12-22-2006 08:24 PM
Finding is a given date is a month end date?? rsujatha@gmail.com Java 10 05-15-2006 06:02 AM
MonthCalendar1 or DateTimePicker1 How to jump for one month or haw to get no of month? Michal Maciejczak C++ 1 04-23-2005 11:18 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57