• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

setFirstDayOfWeek in Calendar  RSS feed

 
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to use MONDAY as the first day of week in a US locale, so I figured I would just get a Calendar instance and set it once, but setFirstDayOfWeek doesn't seem to do anything:



Calendar returns the same value before and after setFirstDayOfWeek.

Any ideas on why this doesn't work and how to get it to work?

Thanks,
Lajos
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set the calendar to today's date. Then you changed the "first day of the week" to Monday instead of Sunday. getFirstDayOfWeek() returns Monday. What are you trying to do?




MONDAY constant = 2
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to change get(Calendar.DAY_OF_WEEK) to return 5 for Friday, instead of 6.

But I guess this is not how it works.
 
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of many ways in which java.util.Calendar is counterintuitive. It does work, but not the way you're expecting. Look at the documentation[ for the DAY_OF_WEEK field: "This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY." When your program prints 6, it's telling you that the day of week is FRIDAY. This constant value has nothing to do with the beginning of the week - it's just a constant that means FRIDAY. It does coincidentally happen to be the same as the day of the week if the first day of the week is SUNDAY (1) - but it doesn't change if the first day of the week is redefined.

Compare this to the API for WEEK_OF_MONTH and WEEK_OF_YEAR, which do say that they depend on the first day of the week. You can test to see if this works correctly for your purposes.

If you really need a number representing day of week with 1 meaning Monday and 7 meaning Sunday, you can get it with a tiny bit of math:

[ October 27, 2006: Message edited by: Jim Yingst ]
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

Thanks for your reply. In the meantime I realized that the days are constants, that's why it will return 6 for FRIDAY, no matter what the first day of the week is set to.

I added the logic to shift the value returned if SUNDAY is the first day of the week.

Thanks,
Lajos
 
a wee bit from the empire
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!