views:

1260

answers:

3

How do you get the first day of week given a Locale using Joda Time?

Point: Most countries use the international standard Monday as first day of week (!). A bunch others use Sunday (notably USA). Others apparently Saturday. Some apparently Wednesday?!

Wikipedia "Seven-day week"#Week_number

A: 

So your question is, how to get the DayOfWeek from a Joda DateTime object? What about this:

DateTime dt = new DateTime().withYear(2009).plusDays(111);
dt.toGregorianCalendar().getFirstDayOfWeek();
Tim Büthe
No, that is not my question.My question is: Given Locale.US (and NOTHING ELSE), what is the first day of the week? And I want Joda Time to tell me - not java std. libraries.Joda Time's chronologies seems to all state that Monday is the first day of the week (which is what that ISO8601 standard says). Is this not possible? Does not Joda Time know that Americans prefer to see their calendars with "Sunday" as the week's first day, while (most of) us Europeans become really upset about that, and want "Monday" to be the start of a week? I find that unbelievable, as Joda Time seems complete.
stolsvik
Why you not just use the Java standard libraries? Maybe they don't implemented it, because it is already there. I really don't get your point.
Tim Büthe
Do you know Joda Time? They would not have needed to implement ANYTHING, as everything IS possible in the java std. libs. My question thus still stands: If it is really not possible with Joda Time, then Joda Time is inclomplete, lacking. I find that hard to believe, but it might be true.
stolsvik
Whatever, the whole discussion seems absolutely pointless to me. Joda Time should, in my opinon, be used because the original implementation is a great PITA. However, for parts that are not provided, you can easily fall back. I don't think it is necessary to duplicate every aspect, just for the sake of completeness. If the firstDayOfWeek method, is really the one and only missing method, I'm getting your point. But I don't understand, why you're so upset and passionated about that...
Tim Büthe
+2  A: 

Seems like you're out of luck, it looks like all of the provided Chronologies inherit the implementation from baseChronology, which supports only ISO definitions, i.e. Monday=1 ... Sunday=7.

You would have to define your own LocaleChronology, possibly modeled on StrictChronology or LenientChronology, add a factory method:

public static LocaleChronology getInstance(Chronology base, Locale locale)

and override the implementation of

public final DateTimeField dayOfWeek()

with a re-implementation of java.util.Calendar.setWeekCountData(Locale desiredLocale) which relies on sun.util.resources.LocaleData..getCalendarData(desiredLocale).

crowne
I don't quite get that. Us Norwegians and them Americans do agree that 2009-11-26 is a Thursday - it is only that when we display a full week - 7 days in a row - us Europeans start with some Monday and finish of with the following Sunday (i.e. "Weekend" being Saturday and Sunday, right?), while Americans start with some Sunday and finish with the following Saturday.This is exactly the same as Norwegians writing 26/11 2009, Swedish 2009-11-26 (they're logical over there!), and Americans 11/26 2009 (?!).It is merely a display issue, not a calculation issue.
stolsvik
All Chronologies will show 2009-11-26 as a Thursday, and this should show as week 48 of 53 in the ISOChronology.Now if we were to ask what is the firstDayOfWeek(48), the answer would depend on the locale:US = Sun 2009-11-22NO = Mon 2009-11-23
crowne
The problem seems similar to different programming languages that use different values for the first position in an array, e.g. java is zero based array[0], whereas pascal and cobol are 1 based array[1].So if you are given a random element > 1 in an array with a size of 7, the way that you determine how to go back to the first position would depend on your programming language:java: fdow(day[3]) = day[3-3]cobol/pascal: fdow(day[3]) = day[3-2]
crowne
I hope that analogy isn't too obscure, similarly the first day of the week in different locales will differ because some go back to mon and others go back to sun, so the calulation is different depending on the locale.
crowne
Thanks for answer and comments!. But my point is still that it does not matter that Monday is set as 1 and Sunday as 7 in Joda Time. For java, Monday is 2, and Sunday 1 (and Saturday 7), and it is like that no matter what Locale you operate under. You can however simply ask a Calendar instantiated with a given Locale "what is customarily the first day of the week for this Locale", and hence make a Calendar-chooser which makes sense to the users of that locale.(My previous comment about this not affecting calculations is not quite true, though: The WEEK NUMBER is affected by this, I believe.)
stolsvik
Where do you have firstDayOfWeek(48)? I didn't quite understand this. You don't need an argument - it would be the same for all 1-53, always? But anyway, the more I look into this, the more it seems like Joda Time simply does not handle this problem? Isn't that exceptionally strange, given that it has been hailed as the replacement of java's Calendar stuff? .. and not tend to people of the USA?
stolsvik
+2  A: 

Joda-Time uses the ISO standard Monday to Sunday week.

It does not have the ability to obtain the first day of week, nor to return the day of week index based on any day other than the standard Monday. Finally, weeks are always calculated wrt ISO rules.

Stephen Colebourne, Joda-Time author

JodaStephen
Then I have to ask - mustn't this be seen as a clear deficiency with Joda Time? Seriously, for "printed calendars" (that is, those you hang on your kitchen, those that you pick from in flight-schedule-webapps), I find it severely annoying when a calendar shows Sunday as first day of week - I do actually get into problems because of it. So I assume that it must be exactly the same for Americans when they are presented with calendars where weeks start on Monday: Everything becomes "one day early", so to speak - when looking at the third "box" of a week, an American assumes it is Tuesday.
stolsvik