java日历setFirstDayOfWeek不工作 [英] java Calendar setFirstDayOfWeek not working
问题描述
以下是真实日历:
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
2015/3/24
如下:
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));
}
}
setFirstDayOfWeek 到 MONDAY
我期望的结果是 2
,但无论我设置的日期每周的第一天
(已尝试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)
的结果也不会影响code> 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?
推荐答案
cal.get(Calendar.DAY_OF_WEEK)
将返回给定日期的那一天(SUNDAY,MONDAY等等)。所以它会返回你TUESDAY然后3,无论一周的第一天是什么。这与 setFirstDayOfWeek
方法无关。
cal.get(Calendar.DAY_OF_WEEK)
will return you which day it is (SUNDAY, MONDAY, etc...) for the given date. So it will return you TUESDAY and then 3, whatever the first day of week is. This has nothing to do with the setFirstDayOfWeek
method.
如果要计算开始后的天数,你只需要使用 getFirstDayOfWeek
获得一周的第一天,并做一些简单的数学计算。
If you want to compute the number of day since the beginning of the week, you just have to get the first day of the week using getFirstDayOfWeek
and do some simple math.
这篇关于java日历setFirstDayOfWeek不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!