Why does the en_GB locale think the 1st of January

2019-04-11 18:27发布

问题:

It turns out that the week-of-year using ww as a java date format string is 52 for the 1st January 2011 when the locale is en_GB. Here is proof (using a scala REPL, though I could have done this using a Java program)

First get my locales

scala> val en = java.util.Locale.getAvailableLocales.find(_.toString == "en") getOrElse error("no en")
en: java.util.Locale = en

scala> val en_GB = java.util.Locale.getAvailableLocales.find(_.toString == "en_GB") getOrElse error("no en_GB")
en_GB: java.util.Locale = en_GB

Now make the 1st of Jan

scala> import java.util.Calendar; import Calendar._
import java.util.Calendar
import Calendar._

scala> Calendar.getInstance
res23: java.util.Calendar = java.util.GregorianCalendar[time=1300708839128,....]

scala> res23.set(MONTH, JANUARY); res23.set(DAY_OF_MONTH, 1)

scala> val firstJan = res23.getTime
firstJan: java.util.Date = Sat Jan 01 12:00:39 GMT 2011

Now declare a method to print this in a locale-dependent way:

scala> def weekInLocale(l : java.util.Locale) = { java.util.Locale.setDefault(l); new java.text.SimpleDateFormat("ww").format(firstJan) }
weekInLocale: (l: java.util.Locale)java.lang.String

Now invoke it:

scala> weekInLocale(en)
res24: java.lang.String = 01

scala> weekInLocale(en_GB)
res26: java.lang.String = 52

Is this right?

回答1:

From ISO8601, week 1 is defined as the week containing January 4th. Since 2011-01-01 was a Saturday, this falls in the previous week.

Since there is no week 0, then 2011-01-01 can also be spelled as 2010-W52-6.

Those wacky Americans, on the other hand, allow for partial weeks. From Wikipedia:

The US system has weeks from Sunday through Saturday, and partial weeks at the beginning and the end of the year.

So, they would define it as being the last day of week 1.



回答2:

for Europe 1 January 2011 W52 is correct.
This is ISO standard for definition of week 01

however various numberings exists for different countries. see Week_numbering



标签: java locale date