l579913
2016-01-05 02:59
采纳率: 0%
浏览 3.4k

关于java中calendar的week_of_year

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cl = Calendar.getInstance();
cl.setFirstDayOfWeek(Calendar.MONDAY);
cl.setMinimalDaysInFirstWeek(3); // 每年的第一周必须大于或等于3天,否则就算上一年的最后一周
cl.set(2015, 0, 1);

System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));
cl.set(2015, 11, 31); // 使用SimpleDateFormat获取的周数是错误的,get(Calendar.WEEK_OF_YEAR)是对的
System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));
cl.set(2006, 0, 1);
System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));
cl.set(2016, 0, 1);
System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));
cl.set(2016, 0, 3);
System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));
cl.set(2016, 0, 4);
System.out.println(sdf.format(cl.getTime())+"------"+cl.get(Calendar.WEEK_OF_YEAR)+"-------"+getWeekOfYear(cl));


输出结果为:
2015-01-01------1-------2015
2015-12-31------1-------2015
2006-01-01------1-------2006
2016-01-01------1-------2016
2016-01-03------1-------2016
2016-01-04------2-------2016

问题来了,为什么2015-12-31的week_of_year为1呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 兔子托尼啊 2016-01-05 03:07
    打赏 评论

相关推荐 更多相似问题