2014-11-03 17 views
2

我實際上正在通過實用類壓延機。 我發現自己混同,在Java中的第一週的最小日子

方法

getFirstDayOfWeek() 
returns the first day of the week; 
e.g., SUNDAY in the U.S., MONDAY in France. 

getMinimalDaysInFirstWeek() 
returns the minimal days required in the first week of the year. e.g., 
if the first week is defined as one that contains the first day of the 
first month of a year, this method returns 1. If the minimal days required 
must be a full week, this method returns 7. 

我住在印度(UTC + 05:30)金奈,加爾各答,孟買,新德里。 在此時區內未觀察到夏令時。

爲了拿到年終的一週,我編寫這樣

Calendar calendar = Calendar.getInstance(); 
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // returns 45 

45只可能是Java的對待

WeekNumber FromDate      ToDate 
Week 01  December 30, 2013    January 5, 2014 // December 30 is Monday 
Week 45  November 3, 2014(Monday)  November 9, 2014 

確實Java的花費最少的天數整整一個星期要爲7 ?因爲我可以通過setMinimalDaysInFirstWeek()來更改它。它會有什麼影響?

+0

你試過了嗎? – arcy 2014-11-03 15:11:09

回答

2

它與週一年的一週中的天數有關。除非你對週年感興趣,否則這可能與你無關。默認情況下,我認爲它是4,因爲這是正常的ISO-8601值。

我不確定是否與「每週的第一天」部分相互作用 - 您需要檢查,但聽起來像是這樣。它不應該有什麼與您的時區。

但是,例如,2014年的週年開始於2013年12月30日,正是因爲這在2014年給了5天。如果您再看看其他幾年,您會發現在1月日曆年的前幾天X在第X-1周的第52周或第53周。

+0

確實是4.文檔很好地說明了它。例如,1998年1月1日是星期四。如果getFirstDayOfWeek()是MONDAY並且getMinimalDaysInFirstWeek()是4(ISO 8601標準兼容設置),那麼1998年的第1周從1997年12月29日開始,到1998年1月4日結束。過去三天如果getFirstDayOfWeek()是SUNDAY,那麼1998年第一週從1998年1月4日開始,到1998年1月10日結束; 1998年的前三天是1997年第53周的一部分,他們的週年是1997年。 – 2014-11-03 18:14:28

相關問題