This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calendar instance DAY_OF_WEEK clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calendar instance DAY_OF_WEEK clarification" Watch "Calendar instance DAY_OF_WEEK clarification" New topic
Author

Calendar instance DAY_OF_WEEK clarification

Sameer K Meher
Greenhorn

Joined: Aug 12, 2012
Posts: 2

While studying Calendar classes, I am struck in one of the code .


Output:

Wed Aug 15 01:11:35 IST 2012
1
7
4

What is the difference between Line no 5 & 6 when the static variable is same DAY_OF_WEEK.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

first of all this code won't compile since there is no import statement for the Calendar class. Assuming you might have missed it the explanation is that DAY_OF_WEEK is a constant(public final static field) in Calendar class. when you did c.DAY_OF_WEEK , the reference variable c is actually getting replaced into Calendar. so c.DAY_OF_WEEK is same as Calendar.DAY_OF_WEEK(you already know that we can access static variable both using Class name and the reference variable(which is a bad practice)). now DAY_OF_WEEK is of type int whose value is 7, SO 7 gets printed. check the values of constants from here http://docs.oracle.com/javase/6/docs/api/constant-values.html#java.util.Calendar.WEDNESDAY

regarding 6th line , i myself got confused. anyways you can check the api here at http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#get(int). research and post the solution if you get.
Rodine Villar
Greenhorn

Joined: Apr 16, 2011
Posts: 6
In calendar class...
public final static int DAY_OF_WEEK = 7;

c.DAY_OF_WEEK = static Day of week int code in calendar class. (the one above)
c.get(Calendar.DAY_OF_WEEK) = get the day today using DAY_OF_WEEK int code.

You can also do c.get(7) because the integer 7 is the code for the DAY_OF_WEEK field of the calendar class but it's not advisable to do so because future updates of the JDK may update the number assigned to DAY_OF_WEEK field to a different number which may cause a lot of java programs to fail but if you use c.get(Calendar.DAY_OF_WEEK) whatever changes to the integer code of DAY_OF_WEEK will not affect your program.


I believe in God not in religion.
Sameer K Meher
Greenhorn

Joined: Aug 12, 2012
Posts: 2

This is what I found out in the documentation:

The calendar field values can be set by calling the set methods. Any field values set in a Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.

which explains that while invoking get methods on Calendar instance, it calculates with the current date set in the Calendar instance.

So, the below code explanation is correct.
c.get(Calendar.DAY_OF_WEEK) = get the day today using DAY_OF_WEEK int code

Thank you...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Rodine Villar wrote:You can also do c.get(7) because the integer 7 is the code for the DAY_OF_WEEK field of the calendar class but it's not advisable to do so because future updates of the JDK may update the number assigned to DAY_OF_WEEK field to a different number

...and because it makes your code unreadable!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar instance DAY_OF_WEEK clarification
 
Similar Threads
Calendar.HOUR for another timezone
How to find day from the week using date format?
return date for a given week number in a month
GregorianCalendar
plz help me, with Calendar problem..