| Author |
Doubt in Calendar.get(int field)
|
priby mathew
Greenhorn
Joined: Jan 28, 2008
Posts: 25
|
|
Greetings to all, I want to know what exactly does the method get(int field)returns. I tried with the following code, but I am not getting any idea about the values returned by the get(int field) method. import java.util.Calendar; class CalendarTest { public static void main(String a[]){ Calendar c= Calendar.getInstance(); int l = c.get(14); System.out.println("l is... "+l); } } Thanks princy
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
If you look at the 'Field Detail' section of the Calendar javadoc, you will find a list of constants that can be passed to the get method and what the return value represents in each case.
|
Joanne
|
 |
priby mathew
Greenhorn
Joined: Jan 28, 2008
Posts: 25
|
|
Hi Joanne, I went through the 'Field Details' section and I was able to find only the constants like ERA, YEAR etc. But I want to know what result would the get(int field) will return if I give the argument as a number. For example Calendar.get(14) Thanks Princy
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
That would depend on whether the number you passed was a valid value or not. If it wasn't a valid value an ArrayIndexOutOfBoundsException would be thrown. If it was a valid value then it would return the appropriate value for the number you passed. You can find out the actual value of each of the constants by looking at the source for the Calendar class. You can find this in the src.zip file in your JDK installation. In the case of the example you gave 14 corresponds to milliseconds.
|
 |
Mark Newton
Ranch Hand
Joined: Jan 31, 2006
Posts: 129
|
|
Just in case you're still not clear, to get the actual values of the constant fields (ERA, YEAR, etc), you need to follow the 'Constant Field Values' link (here), and you'll see that 14 corresponds to MILLISECOND, as Joanne pointed out. The point being that you can use c.get(Calendar.MILLISECOND); which makes your code much more readable.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by David Payne: you need to follow the 'Constant Field Values' link ( here)
Now that's a lot easier than searching through the source code
|
 |
priby mathew
Greenhorn
Joined: Jan 28, 2008
Posts: 25
|
|
Hi Joanne and David, Thanks a lot for your answers. Now the concept is clear to me. Thanks Princy
|
 |
 |
|
|
subject: Doubt in Calendar.get(int field)
|
|
|