简体   繁体   English

不正确的week_of_year

[英]incorrect week_of_year

Have next function to get week of year: 具有下一个功能来获取一年中的一周:

static public Integer getWeek(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setMinimalDaysInFirstWeek(1);
    cal.setTime(date);
    Integer week = cal.WEEK_OF_YEAR;
    Integer month = cal.MONTH;
    if ((week == 1) && (month == 12)) week = 52;
    return week;
}

Call the function with date=02.01.2013 调用日期= 0.201.2013的函数

What I see in debug: 我在调试中看到的是:

  1. date = Wed Jan 02 00:00:00 SAMT 2013 日期= 2013年1月2日星期三00:00:00
  2. week = 3 周= 3
  3. month = 2 月= 2

I want to get: week=1, month=1. 我想得到:week = 1,month = 1。 Right? 对?

Where am I wrong? 我哪里错了?

JRE 1.6 JRE 1.6

Thanks a lot for advance. 非常感谢。

Calendar.WEEK_OF_YEAR and Calendar.MONTH are static constants Calendar uses to look up fields. Calendar.WEEK_OF_YEARCalendar.MONTH是Calendar用于查找字段的静态常量。 You want 你要

Integer week = cal.get(Calendar.WEEK_OF_YEAR);
Integer month = cal.get(Calendar.MONTH);

Also, note that (I think) January is considered month 0. 另外,请注意,(我认为)一月被视为第0个月。

public final static int WEEK_OF_YEAR = 3; is inside source of Calendar. 在日历源中。

When you access WEEK_OF_YEAR it prints value of this field, it would be treated like accessing static field of Calendar class. 当您访问WEEK_OF_YEAR它将打印此字段的值,将其视为访问Calendar类的静态字段。

If you want get week, you need to do Integer week = cal.get(Calendar.WEEK_OF_YEAR); 如果要获取星期,则需要执行Integer week = cal.get(Calendar.WEEK_OF_YEAR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM