File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Question about the day-of-week in GregorianCalendar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about the day-of-week in GregorianCalendar" Watch "Question about the day-of-week in GregorianCalendar" New topic

Question about the day-of-week in GregorianCalendar

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I have the following code which works except that the day-of-the-week is coming out wrong.

Here is the output that I am getting:

[ April 12, 2008: Message edited by: Kaydell Leavitt ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
Please go through the Calendar constructors and API. The problem might be that 0=JANUARY and 1=FEBRUARY so it may be interpreting 4 as MAY.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

With java.text.DateFormatSymbols you can let the JVM do the naming part for you, and even locale specific.

For example:

You can switch the language easily, by using a different locale, such as

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15083

Why do you think that day-of-the-week is wrong; what did you expect, and how is the actual output different from what you expect?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jhakda Velu
Ranch Hand

Joined: Feb 26, 2008
Posts: 167
Campbell is right.
0 represents Jan,so 4 will represent May.
When you pass 2008,4,11 to teh constructor, it has taken it as May 11,2008, which happens to be a Sunday
You have written your own method to get the Month and Day . So you output the Month as April, based on the case statement (case 1 is Jan to you). When you want to get the Day,in the case statement, you have written as CALENDAR.SUNDAY as Sunday and so on.


If I become filthy rich, I'll sponsor research for painless dental treatment at Harvard Medical School. Thats why,I'm learning Java.I have 32 teeth, 22 are man made.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Please note the inconsistency in the Calendar API: weekdays start at 1 (Calendar.SUNDAY) whereas months start at 0 (Calendar.JANUARY). That's why it is always a wise idea to use the constants instead of hard coded values like 4.
I agree. Here's the link:
subject: Question about the day-of-week in GregorianCalendar
It's not a secret anymore!