java 如何让java日历从星期一开始工作日?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28021204/
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
How to make java calendar to start weekday from Monday?
提问by rahul_raj
I have written code, which uses Java calendar and shows the DAY_OF_WEEK from timestamp. But the default calendar starts from Sunday (1). I want it start from Monday eg. It should return 1 for Monday. Here's my code :
我编写了代码,它使用 Java 日历并显示时间戳中的 DAY_OF_WEEK。但默认日历从星期日 (1) 开始。我希望它从星期一开始,例如。它应该在星期一返回 1。这是我的代码:
Calender c = Calender.getInstance(TimeZone.getInstance("Australia/Sydney"));
c.setTimeInMillis(1413831601032L);
c.setFirstDayOfWeek(Calender.Monday);
System.out.println(c.get(c.DAY_OF_WEEK));
setFirstDayOfWeek() doesn't help in this case.
setFirstDayOfWeek() 在这种情况下没有帮助。
The output should be 2 for Tueday, but its showing me 3. Any help will be appreciated.
周二的输出应该是 2,但它向我展示了 3。任何帮助将不胜感激。
回答by Ankur Singhal
The first day of the week is derived from the current locale. If you don't set the locale of the calendar (Calendar.getInstance(Locale)
, or new GregorianCalendar(Locale))
, it will use the system's default.
一周的第一天源自当前语言环境。如果您没有设置日历的区域设置(Calendar.getInstance(Locale)
或新的GregorianCalendar(Locale))
,它将使用系统的默认值。
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek());
}
This should show a different output with different JVM parameters for language/country:
这应该显示具有不同语言/国家/地区的不同 JVM 参数的不同输出:
-Duser.language=en -Duser.country=US -> en_US: 1 (Sunday)
-Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)
Also, You can use the method setFirstDayOfWeek()
to set the first day of the week. The method can only affect the return values of WEEK_OF_MONTH
or WEEK_OF_YEAR
. For DAY_OF_WEEK, it does nothing.
此外,您可以使用该方法setFirstDayOfWeek()
设置一周的第一天。该方法只能影响return values of WEEK_OF_MONTH
或WEEK_OF_YEAR
。F或 DAY_OF_WEEK,它什么都不做。
Refer herefor more
参考这里了解更多
Also if you see the Calendar.java, you will see that values for days is constant, as below. So that's why it will return 1 for MONDAY, no matter what the first day of the week is set to.
此外,如果您看到Calendar.java,您将看到天数的值是恒定的,如下所示。所以这就是为什么它会为 MONDAY 返回 1,无论一周的第一天设置为什么。
public final static int SUNDAY = 1;
public final static int MONDAY = 2; ....
public final static int SATURDAY = 7;
public final static int SUNDAY = 1;
公共最终静态 int MONDAY = 2; ....
公共最终静态 int SATURDAY = 7;
You can do something as below and manipulate the data, according to the first day you are setting.
根据您设置的第一天,您可以执行以下操作并操作数据。
[c.get(Calendar.DAY_OF_WEEK) - 1]);
回答by SubOptimal
Try to avoid to use the raw values returned by get. In your code you should make a check always against the constants defined in the Calendar class. This has the big advantage it's more readable.
尽量避免使用 get 返回的原始值。在您的代码中,您应该始终对照 Calendar 类中定义的常量进行检查。这具有更大的优势,它更具可读性。
Consider following snippets
考虑以下片段
Here it's hard to find for which day of week you want to do which action
在这里很难找到您想在一周中的哪一天执行哪个操作
switch (c.get(Calendar.DAY_OF_WEEK)) {
case 2:
// do something
; break;
case 3:
// do something
break;
}
This example is more self-explaining
这个例子更不言自明
switch (c.get(Calendar.DAY_OF_WEEK)) {
case Calendar.MONDAY:
// do something
; break;
case Calendar.TUESDAY:
// do something
break;
}