Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Why does the calendar object return this number?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Why does the calendar object return this number?

    ok so I needed to check someones birth month. I created a calendar object with current date

    Java Code:
     Calendar cal = Calendar.getInstance();
    getTime() method proves this does indeed say may.

    I wanted to check the month with an employee that I've already created a custom date object for. I need an integer from the calendar object to compare with my custom date object.

    I tried cal.Month
    description: returns int, Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

    however cal.MONTH returns 2. why does it think its march? have I misread the description... if JAN is 0 shouldnt may be 4?
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why does the calendar object return this number?

    cal.MONTH is a static int constant, and actually should be used as Calendar.MONTH; the number, doesn't mean anything by itself, but is used as a parameter for several Calendar object methods.

    For example, you probably want to use Calendar.MONTH in the get(...) method, i.e.,

    Java Code:
          Calendar cal = Calendar.getInstance();
          if (Calendar.MAY == cal.get(Calendar.MONTH)) {
             System.out.println("cal's month is May");
          } else {
             System.out.println("cal's month is not May");
          }
    Last edited by Fubarable; 05-22-2012 at 05:55 PM.
    EscSequenceAlpha likes this.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why does the calendar object return this number?

    Doesn't a Julian calendar start with March? (on the 'idus' (half way) to be exact), so MAR == 0, APR == 1, MAY == 2 ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why does the calendar object return this number?

    I see thanks, thats a very concise explaination

    I'm not good with understanding code I havent made myself yet
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why does the calendar object return this number?

    I did not know this. perhapes I should look this up. that would indeed explain the 2
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why does the calendar object return this number?

    Quote Originally Posted by EscSequenceAlpha View Post
    I did not know this. perhapes I should look this up. that would indeed explain the 2
    No, that would not explain the 2. Again, Calendar.MONTH has nothing to do with March or May, but is nothing more than a constant to be used as a parameter for methods, that's it. The numeric value has no meaning, other than perhaps it was the 3rd constant created for the Calendar class. If the Calendar class were made after enums had been constructed, then Calendar.MONTH would have been an enum and would not have an int value (not counting its cardinality which is a different issue).

    If you look at the constants for Calendar, you'll see that in this progression,

    ERA == 0, YEAR == 1, MONTH == 2, WEEK_OF_YEAR == 3, WEEK_OF_MONTH == 4, DAY_OF_YEAR == 6, DAY_OF_WEEK == 7, DAY_OF_WEEK_IN_MONTH == 8, ...

    Note that numbers are reused, so that JANUARY == 0, FEBRUARY == 1, MARCH == 2, and SUNDAY == 1, MONDAY == 2, ...

    But you should never use these for there numeric values but rather for their logical representation.
    Last edited by Fubarable; 05-22-2012 at 06:38 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why does the calendar object return this number?

    Quote Originally Posted by EscSequenceAlpha View Post
    I did not know this. perhapes I should look this up. that would indeed explain the 2
    Don't take my word for it; I'm extremely good at explaining unexplainable numbers; my old mother feels still ashamed ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Why does the calendar object return this number?

    Quote Originally Posted by Fubarable View Post
    No, that would not explain the 2. Again, Calendar.MONTH has nothing to do with March or May, but is nothing more than a constant to be used as a parameter for methods, that's it. The numeric value has no meaning, other than perhaps it was the 3rd constant created for the Calendar class. If the Calendar class were made after enums had been constructed, then Calendar.MONTH would have been an enum and would not have an int value (not counting its cardinality which is a different issue).
    I thought I understood but after reading it again I'm completely confused... I'm not sure whats going on actually. so lets analyse

    calender.MONTH is a constant to be used as a parameter for methods. right, so what is the value of the constant?
    I think you're saying by itself it doesnt matter, would it be right to think of it as a proprietary language for itself? or something.

    so what does it mean to extract it from my cal object?. does it mean whatever proprietary code for the class that makes "MONTH" is understood by the other calender variables and thus you you check if it equals calendar.MAY?

    I'm sorry this is very confusing stuff for me, I find it hard to visual things that arnt physical
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why does the calendar object return this number?

    The constant is "understood" by the methods that accept it as a parameter. For instance, the Calendar#get(...) method understands that if you pass 2 in, you are requesting the month of that Calendar object, if you pass 0 in, the era, if 1, the year, etc... The code inside of get is wired to read in this way. But you the coder shouldn't worry what the values of these constants are as long as you use the correct constant. For instance, Java could have decided that the value of Calendar.MONTH could be -20381, but that wouldn't result in the least change in your code, since you'd still use it the same way: myCalendar.get(Calendar.MONTH), and the get method would still know what MONTH's value meant.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,180
    Rep Power
    20

    Default Re: Why does the calendar object return this number?

    Instead of providing individual getMonth(), getDay(), getYear() methods, Calendar provides a single get() method with a parameter that is used to tell it which field you want to get. That parameter is one of those constants Fubarable is talking about.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Why does the calendar object return this number?

    My suggestion is, take a look at the java.util.Calender class source code. Read the available documentation in the source code, hopefully you can understand better how the Calender object works. A lot of people said that this date / calender implementation is one of the poor side the have in Java, the API is not user friendly. Sometimes it is hard to understand at the first time, especially with a beginner to Java.

    As an alternative you can have a look on other implementation of date / calender library such as the Joda Time (Java date and time API - Home). The Joda Time have a simpler ways when working with date and time in Java.

Similar Threads

  1. Input a word, return a number
    By B-52 Stratofortress in forum New To Java
    Replies: 7
    Last Post: 05-15-2012, 03:39 AM
  2. Calendar object? - Getting date from user
    By hiei_yasha in forum New To Java
    Replies: 9
    Last Post: 01-23-2011, 04:58 AM
  3. Create new Calendar object???
    By aliencc in forum New To Java
    Replies: 4
    Last Post: 01-17-2011, 02:19 PM
  4. how do i make a string return a number?
    By pjr5043 in forum New To Java
    Replies: 6
    Last Post: 09-15-2008, 05:56 AM
  5. calendar object in preparedStatement
    By Heather in forum SWT / JFace
    Replies: 3
    Last Post: 06-30-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •