Taking a look at the source code of java.time.DayOfWeek
reveals:
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster {
/**
* The singleton instance for the day-of-week of Monday.
* This has the numeric value of {@code 1}.
*/
MONDAY,
/**
* The singleton instance for the day-of-week of Tuesday.
* This has the numeric value of {@code 2}.
*/
TUESDAY,
// ..
}
So how can DayOfWeek.MONDAY = 1
? I am asking because I use GWT which does not support the new java.time
stuff yet (or never will be idk). So what I did I made my own version of DayOfWeek
but that one got a public int getValue() { return ordinal() + 1; }
which is really annoying since I have to call it everytime.
That is why I am curious why the above version starts with 1. The other thing I'd like to know is why the f this has to start with 1 and not with 0 just like every other enum does. They could have gone for Monday = 0
, Tuesday = 1
, etc. but no! They instead switched from Sunday = 0
, Monday = 1
, etc. to that thing up there.
No, this is not an option
public enum MyDayOfWeek {
DUMMY,
MONDAY, TUESDAY, // ..
;
}
Simply because this would be annoying:
for(MyDayOfWeek day : MyDayOfWeek.value() {
if(MyDayOfWeek.DUMMY == day) {
continue;
}
}
[SUMMARY]
If user-defined enums always start with the first element having the value '0' then how does the first element of the java.time.DayOfWeek
enum start with '1', and is there a way to have my enum start with '1' instead of '0'?