当我执行下面的代码时,我得到相同的输出。
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date procDate = sdf.parse("2016-01-01");
Calendar cal = Calendar.getInstance(Locale.UK);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.setTime(procDate);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
procDate = sdf.parse("2016-12-27");
cal.setTime(procDate);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
}
实际输出:
52
2016
52个
2016
预期产出:
52
2015年
52个
2016
或者
0
2016
52个
2016
Locale
和setFirstDayOfWeek
是我无法改变的。
答案 0 :(得分:0)
让我引用消息来源:
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* week number within the current year. The first week of the year, as
* defined by <code>getFirstDayOfWeek()</code> and
* <code>getMinimalDaysInFirstWeek()</code>, has value 1. Subclasses define
* the value of <code>WEEK_OF_YEAR</code> for days before the first week of
* the year.
*
* @see #getFirstDayOfWeek
* @see #getMinimalDaysInFirstWeek
*/
让我们看看这个实验:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date procDate = sdf.parse("2016-01-01");
Calendar cal = Calendar.getInstance(Locale.UK);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.setTime(procDate);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
cal.add(Calendar.DAY_OF_YEAR, 1);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
cal.add(Calendar.DAY_OF_YEAR, 1);
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
procDate = sdf.parse("2016-12-27");
cal.setTime(procDate);
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
}
52
2016
52个
2016
3
1
2016
52个
2016
WEEK_OF_YEAR
在一年中的整周的第一天设置为1。在此之前,它取决于实施。在默认的Java实现中,默认情况下为52。您可以设置cal.setMinimalDaysInFirstWeek(1);
然后它应该修复您的情况。
为什么呢?询问Sun的人。
答案 1 :(得分:0)
Calendar类不知道您的
调用cal.get(Calendar.WEEK_OF_YEAR);
cal.get(Calendar.YEAR);
应该是一起关系。 因为在X年的最后一周,你可能意味着你已经在X + 1年级,你所看到的输出实际上是正确的。
要想要你想要的东西你应该使用另一个DateFormater打印出一年中的一周以及相应的年份:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("w Y");
Date procDate = sdf.parse("2016-01-01");
System.out.println(sdf2.format(procDate));
procDate = sdf.parse("2016-12-27");
System.out.println(sdf2.format(procDate));
结果:
53 2015
52 2016
答案 2 :(得分:0)
看到这个答案
Understanding java.util.Calendar WEEK_OF_YEAR
这个问题与java一年中不合理使用这个星期有关。
无论如何,调用cal.setMinimalDaysInFirstWeek(3);
将修复结果(就差异而言。在我的情况下,1月stil给了我第53周(!)
答案 3 :(得分:0)
回到本周的开始日我得到了预期的结果。在我的情况下,星期日是一周的开始日。所以我设置了cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
,这解决了我的问题。
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date procDate = sdf.parse("2016-01-01");
Calendar cal = Calendar.getInstance(Locale.UK);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.setTime(procDate);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //Starting day of week
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
procDate = sdf.parse("2016-12-27");
cal.setTime(procDate);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); //Starting day of week
System.out.println(cal.get(Calendar.WEEK_OF_YEAR) );
System.out.println(cal.get(Calendar.YEAR) );
}
输出:
52
2015年
52个
2016