wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value" Watch "Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value" New topic
Author

Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value

Roberta Fine
Greenhorn

Joined: Apr 23, 2013
Posts: 27
My question is about the the difference between the DayOfWeek ENUM and the DAY_OF_WEEK field in terms of the values they hold.
This is what I have learned so far, and code is below:
-------------------------------------
The abstract class java.util.Calendar has the Field:
public static final int DAY_OF_WEEK Field Number for Get and Set indicating the day of the week.
This Field takes Values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
The Constant Field Values for each of these days is as follows:

SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4, THURSDAY =5, FRIDAY =6, and SATURDAY =7.
This info is found on: http://download.java.net/jdk7/archive/b123/docs/api/constant-values.html#java.util.Calendar.DAY_OF_WEEK.
-------------------------------------
Then there is the DayOfWeek ENUM.
java.lang.Object
java.lang.Enum<DayOfWeek>
javax.time.calendar.DayOfWeek

It consists of Seven Constants that describe the days of the week MONDAY through SUNDAY.
The Integer values of the DayOfWeek constants range from MONDAY = 1 through SUNDAY = 7.
This is off by one day backwards from the Constant Field Values noted above.
.
And per the link, using the defined constants (DayOfWeek.FRIDAY) makes your code more readable. ( per http://docs.oracle.com/javase/tutorial/datetime/iso/enum.html)

Also, per http://cr.openjdk.java.net/~sherman/threeten/old/javax/time/calendar/DayOfWeek.html#MONDAY,
The ENUM Constant Detail says this about MONDAY:

public static final DayOfWeek MONDAY

The singleton instance for the day-of-week of Monday. This has the numeric value of 1.

---------------------------------
So, provided you don't manipulate the fields:
If you create an object using the Calendar class, you will actually get an object of the concrete class GregorianCalendar.
If you call the time using getTime() and then the DAY_OF_WEEK using the get method, it will display the current day of the week
with the Constant Field Value int rather than the ENUM numeric value noted above.
So, if it's Monday, the compiler will display a Constant Field int Value of 2.

(Code to follow)
----------------------------------
So, my question is what is the difference between the DayOfWeek ENUM and the DAY_OF_WEEK field in terms of the values they hold?
I realize that the ENUM has singleton instances which I don't really understand, and the DAY_OF_WEEK has Constant Field Values which I basically understand.
Please explain, thanks in advance:



----------------------
OUTPUT:
This is formatted time of OBJECT myCalendar with dot operator and getTime() method:
Fri Oct 04 17:39:54 GMT-08:00 2013

This is built in FIRST DAY OF THE WEEK: 1
This is the text form of the CURRENT DAY OF THE WEEK : Friday
This is the int form of the CURRENT DAY OF THE WEEK: 6
This is the CURRENT DAY OF THE MONTH: 4





Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Those fields were in place (I think) before enums were introduced. They are all ints, whereas elements of an enum are full‑blown objects.
Careful examination of the link you posted shows it says “since 1.8”, so there is another difference: the enum doesn't actually exist. It is planned for Java8. I think they are going to introduce new date and time classes; anybody who has used java.util.Date will say
About *** time, too!
Note the new enums start from JANUARY=1 rather than 0.

Remember you cannot manipulate those fields: in the Calendar class they are final, and the enum is immutable.I think that fields in enums are implicitly final, but not sure; you would have to check in the Java Language Specification.
Roberta Fine
Greenhorn

Joined: Apr 23, 2013
Posts: 27
Thank you for pointing out the 1.8 version aspect of my question about the DayOfWeek enum, I would not have known to look for that.
And, I have read many posts discussing the various issues with Date and Calendar classes, so I hear you on "it's about time..."...; )

What I would like to know is if you recommend creating my own ENUM and coding with it rather than getting it per this example:

------------------------------------
This link: http://www.ehow.com/way_5776652_java-1_5-enum-tutorial.html talks about this idea.
They post the following "code" as an example with reasons why it's preferable:



The link basically says:
The above code, besides being cleaner and more readable, addresses problems with Constant Field Values such as other classes ignoring the constants,
and using their own constants.
With the enum, clients are forced to use only the enumerated values themselves, and debuggers will show the enumerated value’s name and not an integer.
In addition, enums have a number of other advantages. For example, they can be used with the new for-each loop feature which was added in Java 1.5.0.
The Calendar class could include a method like this which iterates through all the days and prints out their names.


--------------------

The main goal for me is to have the first day of the week be Monday as 1 through Sunday as 7. The confusion for me is that the ENUM would technically
have the values of 0 - 6 per the index values.

-------------------------------------------
My last question is about "singleton instance." What exactly does that mean in regard to the upcoming DayOfWeek ENUM?
How does it work in relation to the Constant Field Values already in force with DAY_OF_WEEK?
Thanks in advance for your response.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Roberta Fine wrote: . . . This link: http://www.ehow.com/way_5776652_java-1_5-enum-tutorial.html . . .
…is pretty useless. Try the Java Tutorials, which is much better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Roberta Fine wrote: . . . What I would like to know is if you recommend creating my own ENUM and coding with it rather than getting it per this example:
. . .
No, you shouldn't. If the Calendar class has been designed to work with its own int fields, you should use those int fields. There are other ways to do it; Google for Yoda Time, which is an alternative implementation. the Calendar class would have been a lot better if it had used enums, but they were not available when Calendar was written. They were not available in other languages that I know about, either. The only enumerated types I know about from back then were series of ints, so that would have made no difference.
If you are using an enum, you should usually forget that there are numbers associated with it. Since enum elements are true singletons, and (at least I think they are) constants, you can use the == operator, as it says in the Java Language Specification. Read that and see whether it helps explain about singletons.
Roberta Fine
Greenhorn

Joined: Apr 23, 2013
Posts: 27
K Sheriff, thank you for all of the guidance and feedback, I learned a lot.
Until the next round up, have a good one.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
You're welcome
 
Consider Paul's rocket mass heater.
 
subject: Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value
 
Similar Threads
Calendar class
How to find day from the week using date format?
Confusion in understanding DAY_OF_WEEK
setFirstDayOfWeek in Calendar
Bug with java.util.Calendar