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. Advertisements

  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. Advertisements

  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. Advertisements

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. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,786
    Michael Borgwardt
    May 30, 2004
  2. Replies:
    10
    Views:
    1,092
    Dale King
    May 15, 2006
  3. Michal Maciejczak
    Replies:
    1
    Views:
    2,163
    Matthias Kaeppler
    Apr 23, 2005
  4. Bill
    Replies:
    10
    Views:
    963
    Fulvio
    Apr 13, 2006
  5. Replies:
    1
    Views:
    634
    Steve W. Jackson
    Dec 22, 2006
  6. Mufasa
    Replies:
    0
    Views:
    584
    Mufasa
    Sep 5, 2008
  7. eddie wang

    to convert a month to previous month

    eddie wang, Oct 9, 2003, in forum: ASP General
    Replies:
    9
    Views:
    332
    Chris Hohmann
    Oct 10, 2003
  8. David Morgan
    Replies:
    2
    Views:
    255
    Manohar Kamath [MVP]
    Feb 24, 2004
Loading...