java Calendar.getActualMaximum(Calendar.WEEK_OF_YEAR) 怪异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2047673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calendar.getActualMaximum(Calendar.WEEK_OF_YEAR) weirdness
提问by Epaga
Either I don't understand the method getActualMaximum(int)or the field WEEK_OF_YEAR, or there's a Sun bug involved (or all three)...could someone explain to me why (at least in a German locale...) the following code:
要么我不理解该方法getActualMaximum(int)或字段 WEEK_OF_YEAR,要么涉及 Sun 错误(或所有三个)......有人可以向我解释为什么(至少在德国语言环境......)以下代码:
Locale.setDefault( Locale.GERMAN );
Calendar c = Calendar.getInstance();
c.set( Calendar.YEAR, 2010 );
c.set( Calendar.MONTH, 0 );
c.set( Calendar.DAY_OF_MONTH, 1 );
System.out.println("max: "+c.getActualMaximum( Calendar.WEEK_OF_YEAR ));
System.out.println("actual: "+c.get( Calendar.WEEK_OF_YEAR ));
produces the following output:
产生以下输出:
max: 52
actual: 53
Here's the Javadoc of getActualMaximum(int):
这是的Javadoc getActualMaximum(int):
Returns the maximum value that the specified calendar field could have, given the time value of this Calendar. For example, the actual maximum value of the MONTH field is 12 in some years, and 13 in other years in the Hebrew calendar system.
给定此日历的时间值,返回指定日历字段可能具有的最大值。例如,在希伯来日历系统中,MONTH 字段的实际最大值在某些年份为 12,而在其他年份为 13。
Edit
编辑
The plot thickens. In an English locale(-Duser.language=en -Duser.country=us) the output is:
情节变厚了。在英语语言环境( -Duser.language=en -Duser.country=us) 中,输出为:
max: 52
actual: 1
Seems to point to it being a Sun bug for German locales?
似乎指出它是德国语言环境的 Sun 错误?
回答by Salandur
This information is correct:
此信息是正确的:
max: 52
actual: 53
The year 2010 has a maximum of 52 weeks. The actual week is 53, since 2009 has maximum 53 weeks and most weeks start on sunday or monday. Week 1 is in most cases the first week of the year with 4 days in january. Since the week of the january 1st has only 2 or 3 days in 2010, the week is considered part of 2009.
2010 年最多有 52 周。实际周数是 53,因为 2009 年最多有 53 周,大多数周从星期日或星期一开始。在大多数情况下,第 1 周是一年中的第一周,一月有 4 天。由于 1 月 1 日这一周在 2010 年只有 2 或 3 天,因此该周被视为 2009 年的一部分。
Most likely the english locale has different rules for determing week 1, like the first week is the week of january 1st.
很可能英语语言环境对确定第 1 周有不同的规则,例如第一周是 1 月 1 日的那一周。
Wikipedia explains it correctly: wikipedia week article
维基百科正确解释:维基百科周文章
回答by jarnbjo
The problem is, that January 1st 2010 is in week 53 of 2009 (in Germany), but year 2010 only has 52 weeks (December 31st 2010 is in week 52). The Java Calendar object unfortunately does not have a field for the year, to which the week number relates.
问题是,2010 年 1 月 1 日在 2009 年的第 53 周(在德国),但 2010 年只有 52 周(2010 年 12 月 31 日在第 52 周)。不幸的是,Java Calendar 对象没有与周数相关的年份字段。

