Forums Register Login

Java DateFormatSymbols using getWeekdays() method shows Text version of day of week

+Pie Number of slices to send: Send
This is a follow up post to my other post about the Calendar Class ENUM DayOfWeek question.

***My question would be to ask for feedback from the moderators regarding the value of this class and method in creating calendar related applications***

This is new information and solves (for me) the purpose of outputting the TEXT form of the days of the week using the class DateFormatSymbols, and
the getWeekdays() method in addition to a for loop. Code example at the end of this post.

Per http://docs.oracle.com/javase/7/docs/api/java/text/DateFormatSymbols.html,
"DateFormatSymbols is a public class for encapsulating localizable date-time formatting data, such as the names of the months, the names of the days of the week, and the time zone data. DateFormat and SimpleDateFormat both use DateFormatSymbols to encapsulate this information."

Per http://devmanuals.com/tutorials/java/corejava/DateTimes/DateFormatSymbolsgetWeekdays.html,
"The getWeekdays() method returns a string array of each weekdays' name."

The thing to note is that the CONSTANT FIELD VALUES for the days of the week start with SUNDAY = 1 thru SATURDAY = 7. There is no 0th value.
The String array that holds the text version of the days of the week does have a 0th element, however, it is empty.
Thus, the for loop should start with -1- to begin with SUNDAY.
Here is the code per the link above:



Output is:
Full name of day : Sunday
Full name of day : Monday
Full name of day : Tuesday
Full name of day : Wednesday
Full name of day : Thursday
Full name of day : Friday
Full name of day : Saturday

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

For me, this brings the process of using the Calendar and Date classes along with the related classes for formatting the output of the fields full circle.
+Pie Number of slices to send: Send
 

Roberta Fine wrote:Thus, the for loop should start with -1- to begin with SUNDAY.


Not necessarily. What about simply:

For me, this brings the process of using the Calendar and Date classes along with the related classes for formatting the output of the fields full circle.


I suspect it may have something to do with the flak they got for making Calendar.MONTH 0-based, which causes no end of mistakes. Far better, I reckon, to have a wasted array element than force everyone to use a convention they don't use anywhere else (although I'd also say that it's much less important with day numbers than it is with months).

My 2¢

Winston
+Pie Number of slices to send: Send
Roberta

There have been a number of problems over the years using Calendar and Date. Hence, why developers look for other options like Joda Time and DateUtils. I would be surprised to find a junior Java developer that didn't fall into one of the many pitfalls with these classes.

Personally, I am looking forward to getting to grips with Java 8 Date and Time APIs.
+Pie Number of slices to send: Send
Hello Bartenders,

Thank you for the feedback. I like the alternate loop method Winston, I'll keep that in my toolkit.
And James as for Joda-Time and DateUtils classes, I'll look into those next and see how they compare.
As it turns out, because of both the simplicity and complexity (including pitfalls) of these classes, I have learned a LOT about
abstraction, encapsulation, inheritance, getters and setters, and manipulating displays to be "human readable."
And, I'm thinking that's a good thing out here on the ranch....
Thank you again pardners!

get schwifty. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2625 times.
Similar Threads
About GregorianCalendar
Exception in thread "main" java.lang.NullPointerException
Bug with java.util.Calendar
Calendar class ENUM DayOfWeek Constant Detail vs. DAY_OF_WEEK Constant Field Value
Dynamic Table
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:30:40.