Java 为什么我的 GregorianCalendar 对象返回错误的星期几?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19136226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my GregorianCalendar object return the wrong day of week?
提问by jumps4fun
My issue is seemingly extremely simple. I make a calendar graphic user interface, from a GregorianCalendar object, and uses it's methods to calculate the correct number of days in the different months, and the different date's corresponding weekdays.
我的问题似乎非常简单。我从 GregorianCalendar 对象制作了一个日历图形用户界面,并使用它的方法来计算不同月份的正确天数,以及不同日期对应的工作日。
But the weekdays are consistentyl one day off. The Calendar claims that the 1st of July 2013 is a '2', which in my part of the world means tuesday. It should have been a '1' for Monday. "Easy!" i think, and put in the line: c.setFirstDayOfWeek(Calendar.MONDAY); But no reaction is given.
但平日是一致的一天休息。日历声称 2013 年 7 月 1 日是“2”,在我所在的地区,这意味着星期二。星期一应该是“1”。“简单!” 我想,并把它放在一行: c.setFirstDayOfWeek(Calendar.MONDAY); 但没有给出任何反应。
So I search stackoverflow for an answer, but everyone with my problem seem to have forgotten that January is 0, and not 1. I haven't. So now I am stuck.
所以我在 stackoverflow 中寻找答案,但遇到我的问题的每个人似乎都忘记了一月是 0,而不是 1。我没有。所以现在我被困住了。
As a simplifyed code, I have made a very short code piece, with it's corresponding output:
作为简化的代码,我制作了一个非常短的代码片段,并带有相应的输出:
GregorianCalendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.MONTH, 6);
c.set(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.YEAR, 2013);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY");
System.out.println(sdf.format(c.getTime()));
System.out.println(c.get(Calendar.DAY_OF_WEEK));
and the output is:
输出是:
01-07-2013
01-07-2013
2
2
I refuse to put in a "-1" in my code, to wrongly correct the symptoms of what is obviously a mistake. Help is appreciated.
我拒绝在我的代码中输入“-1”,以错误地纠正明显错误的症状。帮助表示赞赏。
采纳答案by Jon Skeet
I refuse to put in a "-1" in my code, to wrongly correct the symptoms of what is obviously a mistake.
我拒绝在我的代码中输入“-1”,以错误地纠正明显错误的症状。
The mistake is your assumption that Calendar.get(Calendar.DAY_OF_WEEK)
is localized. It isn't. The mapping between day-of-week and number is fixed; use Calendar.getFirstDayOfWeek()
to determine the human understanding of "first day of the week" if you need to; I'd be surprised if you really wanted to show a user "2" anyway... surely you'd want to show them the nameof the day of the week.
错误在于您的假设Calendar.get(Calendar.DAY_OF_WEEK)
是本地化的。不是。星期几和数字之间的映射是固定的;使用Calendar.getFirstDayOfWeek()
来确定人的理解“一周的第一天,”如果你需要的; 如果您真的想向用户显示“2”,我会感到惊讶……当然,您想向他们显示星期几的名称。
Any calculationsinvolving the start of the week should use getFirstDayOfWeek
though.
任何涉及一周开始的计算都应该使用getFirstDayOfWeek
。
回答by ppeterka
Yes, date handling in Java is problematic...
是的,Java 中的日期处理是有问题的...
- Months start from 0 (JANUARY)
- days of week start from
SUNDAY
being 1,SATURDAY
being seven (Ideone fiddle) c.setFirstDayOfWeek(Calendar.MONDAY); is a bit different than what the name suggests
The first week of a month or year is defined as the earliest seven day period beginning on getFirstDayOfWeek() and containing at least getMinimalDaysInFirstWeek() days of that month or year
- 月份从 0 开始(一月)
- 星期几从
SUNDAY
1开始,从SATURDAY
7 开始(Ideone 小提琴) c.setFirstDayOfWeek(Calendar.MONDAY); 与名称所暗示的有点不同
一个月或一年的第一周定义为从 getFirstDayOfWeek() 开始并至少包含该月或一年的 getMinimalDaysInFirstWeek() 天的最早 7 天时间段
You can get out of troubles by alwaysusing the constants defined in the Calendar class, and not even trying to deduce any meaning from the numerical representationsof those constants, or the results returned by the Calendar.get(int)
method...
您可以通过始终使用 Calendar 类中定义的常量来摆脱麻烦,甚至不试图从这些常量的数字表示或Calendar.get(int)
方法返回的结果中推断出任何含义......
回答by CS Pei
This is one of the caveats in Java,
这是 Java 中的警告之一,
DAY_OF_WEEK,
DAY_OF_WEEK,
This field takes values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
此字段采用值 SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY 和 SATURDAY。
When your program prints 2, it's telling you that the day of week is MONDAY. This constant value has nothing to do with the beginning of the week. It does coincidentally happen to be the same as the day of the week if the first day of the week is SUNDAY - but it doesn't change if the first day of the week is redefined.
当您的程序打印 2 时,它告诉您星期几是 MONDAY。这个常数值与一周的开始无关。巧合的是,如果一周的第一天是 SUNDAY,它恰好与一周中的某一天相同 - 但如果重新定义一周的第一天,它不会改变。