Why does the en_GB locale think the 1st of January

2019-04-11 18:32发布

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?

标签: java locale date
2条回答
老娘就宠你
2楼-- · 2019-04-11 19:04

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-11 19:07

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

查看更多
登录 后发表回答