• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Calendar instance DAY_OF_WEEK clarification

 
Greenhorn
Posts: 2
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sameer K Meher
Greenhorn
Posts: 2
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 can't beleive you just said that. Now I need to calm down with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic