簡體   English   中英

不正確的week_of_year

[英]incorrect 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;
}

調用日期= 0.201.2013的函數

我在調試中看到的是:

  1. 日期= 2013年1月2日星期三00:00:00
  2. 周= 3
  3. 月= 2

我想得到:week = 1,month = 1。 對?

我哪里錯了?

JRE 1.6

非常感謝。

Calendar.WEEK_OF_YEARCalendar.MONTH是Calendar用於查找字段的靜態常量。 你要

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

另外,請注意,(我認為)一月被視為第0個月。

public final static int WEEK_OF_YEAR = 3; 在日歷源中。

當您訪問WEEK_OF_YEAR它將打印此字段的值,將其視為訪問Calendar類的靜態字段。

如果要獲取星期,則需要執行Integer week = cal.get(Calendar.WEEK_OF_YEAR);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM