现在是真正的日历:
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?
LocalDate.of( 2015 , Month.MARCH , 24 ) // `LocalDate` object for 2015-03-24.
.getDayOfWeek() // DayOfWeek.TUESDAY constant object
.getValue() // 2
Calendar
是一团糟,它的兄弟类也是如此.幸运的是,这些旧的日期时间类现在已被 java.time 类所取代.
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?
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模板网!