Results 1 to 11 of 11
- 05-22-2012, 04:46 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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
getTime() method proves this does indeed say may.Java Code:Calendar cal = Calendar.getInstance();
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.
-
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 04:55 PM.
- 05-22-2012, 04:55 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-22-2012, 05:26 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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 yetLegend 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.
- 05-22-2012, 05:28 PM #5
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
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.
-
Re: Why does the calendar object return this number?
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 05:38 PM.
- 05-22-2012, 05:33 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Why does the calendar object return this number?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-22-2012, 05:57 PM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why does the calendar object return this number?
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 physicalLegend 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.
-
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.
- 05-23-2012, 09:41 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 05-23-2012, 10:47 AM #11
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.Website: Learn Java by Examples
Similar Threads
-
Input a word, return a number
By B-52 Stratofortress in forum New To JavaReplies: 7Last Post: 05-15-2012, 02:39 AM -
Calendar object? - Getting date from user
By hiei_yasha in forum New To JavaReplies: 9Last Post: 01-23-2011, 03:58 AM -
Create new Calendar object???
By aliencc in forum New To JavaReplies: 4Last Post: 01-17-2011, 01:19 PM -
how do i make a string return a number?
By pjr5043 in forum New To JavaReplies: 6Last Post: 09-15-2008, 04:56 AM -
calendar object in preparedStatement
By Heather in forum SWT / JFaceReplies: 3Last Post: 06-30-2007, 02:47 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks