問題描述
現在是真正的日歷:
March 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
我得到 DAY_OF_WEEK
of 2015/3/24
像這樣:
And I get DAY_OF_WEEK
of 2015/3/24
like this:
public class TestCalendar {
public static void main(String[] argvs){
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(2015,Calendar.MARCH,24);
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
}
}
因為我有 cal.setFirstDayOfWeek
到 MONDAY
,所以我期望的結果是 2
,但是無論哪一天我都設置為 first星期幾
(已嘗試過 SUNDAY 和其他人).它一直向我顯示相同的結果,即 3
.所以看來 firstDayOfWeek
不會影響結果.
Since I have cal.setFirstDayOfWeek
to MONDAY
the result I expecting is 2
, but Whatever day I set to the first day of week
(have tried SUNDAY and others) .It kept show me the same result which is 3
. So It seemed that firstDayOfWeek
won't affect the result.
我是不是做錯了什么?
編輯
我剛剛想到并感謝下面的答案,這個 setFirstDayOfWeek
不會影響 get(Calendar.DAY_OF_WEEK)
也不會影響 get(Calendar.WEEK_OF_YEAR)
I just figured and thanks to answers below, that this setFirstDayOfWeek
will not affect the result of get(Calendar.DAY_OF_WEEK)
nor get(Calendar.WEEK_OF_YEAR)
那么setFirstDayOfWeek()
這個方法是為什么呢?我的意思是我如何告訴程序我希望 2015/3/29
成為第 12 周的最后一天,而不是將其視為第 13 周的第一天?
Then what is this method setFirstDayOfWeek()
designed for?
I mean How can I told the program that I want 2015/3/29
be the last day of the 12th week instead of treating it as the first day of the 13th week?
推薦答案
tl;dr
LocalDate.of( 2015 , Month.MARCH , 24 ) // `LocalDate` object for 2015-03-24.
.getDayOfWeek() // DayOfWeek.TUESDAY constant object
.getValue() // 2
避免使用舊的日期時間類
Calendar
是一團糟,它的兄弟類也是如此.幸運的是,這些舊的日期時間類現在已被 java.time 類所取代.
Avoid legacy date-time classes
Calendar
is a ugly mess, as are its sibling classes. Fortunately these old date-time classes are now legacy, supplanted by the java.time classes.
如果您希望星期一作為一周的第一天,星期日是最后一天,編號為 1-7,然后使用 ISO 8601 日歷在 java.time 類中默認使用.
If you want Monday as the first day of the week, Sunday the last, numbered 1-7, then use the ISO 8601 calendar used by default in the java.time classes.
DayOfWeek
枚舉為一周中的每個 ISO 天保存預定義的對象.如果需要,您可以詢問其數量,但通常最好傳遞此枚舉的對象而不是單純的整數.
The DayOfWeek
enum hold predefined objects for each of those ISO days of the week. You can interrogate for its number if need be, though generally better to pass around objects of this enum rather than mere integers.
LocalDate
類表示沒有時間和時區的僅日期值.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate ld = LocalDate.of( 2015 , Month.MARCH , 24 );
DayOfWeek dow = ld.getDayOfWeek();
int value = dow.getValue(); // 1-7 for Monday-Sunday. But often better to use the `DayOfWeek` object rather than a mere integer number.
有關星期一不是第一天的一周的其他定義,請參閱 WeekFields
類.
For working with other definitions of a week where Monday is not day number one, see the WeekFields
class.
java.time 框架內置于 Java 8 及更高版本.這些類取代了麻煩的舊 legacy 日期時間類,例如 java.util.Date
, 日歷
, &SimpleDateFormat
.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
Joda-Time 項目,現在在 維護模式,建議遷移到 java.time 類.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
要了解更多信息,請參閱 Oracle 教程.并在 Stack Overflow 上搜索許多示例和解釋.規范是 JSR 310.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
從哪里獲得 java.time 類?
Where to obtain the java.time classes?
- Java SE 8 和 SE 9 及更高版本
- 內置.
- 標準 Java API 的一部分,帶有捆綁實現.
- Java 9 添加了一些小功能和修復.
- 大部分 java.time 功能都向后移植到 Java 6 &7 在 ThreeTen-Backport.
- ThreeTenABP項目適應ThreeTen-Backport(上面提到過)專門用于 Android.
- 請參閱如何使用 ThreeTenABP….
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
ThreeTen-Extra 項目通過附加類擴展了 java.time.該項目是未來可能添加到 java.time 的試驗場.您可以在這里找到一些有用的類,例如
間隔
,YearWeek
,YearQuarter
和 更多.The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.這篇關于java日歷setFirstDayOfWeek不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!