Getting a month from a date

Discussion in 'Java' started by Tim Slattery, Nov 8, 2004.

  1. Tim Slattery

    Tim Slattery Guest

    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
    Tim Slattery, Nov 8, 2004
    #1
    1. Advertising

  2. Tim Slattery

    Carl Guest

    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/api/java/util/Calendar.html
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#MONTH
    Carl, Nov 8, 2004
    #2
    1. Advertising

  3. Tim Slattery

    Sudsy Guest

    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.
    Sudsy, Nov 9, 2004
    #3
  4. Tim Slattery

    Jacob Guest

    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.
    Jacob, Nov 9, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Maciejczak
    Replies:
    1
    Views:
    1,980
    Matthias Kaeppler
    Apr 23, 2005
  2. Bill
    Replies:
    10
    Views:
    732
    Fulvio
    Apr 13, 2006
  3. Replies:
    1
    Views:
    445
    Steve W. Jackson
    Dec 22, 2006
  4. Mufasa
    Replies:
    0
    Views:
    398
    Mufasa
    Sep 5, 2008
  5. VYAS ASHISH M-NTB837

    RE: Convert month name to month number faster

    VYAS ASHISH M-NTB837, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    617
    VYAS ASHISH M-NTB837
    Jan 6, 2010
Loading...

Share This Page