• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about the day-of-week in GregorianCalendar

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Locale.FRENCH
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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.


Jhakda
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic