如何使用 JVM 参数为 java.util.Calendar 指定 firstDayOfWeek
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/269486/
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 specify firstDayOfWeek for java.util.Calendar using a JVM argument
提问by user35172
I'm trying to change default firstDayOfWeek for java.util.Calendar from SUNDAY to MONDAY. Is it possible to achieve this through JVM configuration instead of adding this piece of code?
我正在尝试将 java.util.Calendar 的默认 firstDayOfWeek 从 SUNDAY 更改为 MONDAY。是否可以通过JVM配置而不是添加这段代码来实现这一点?
cal.setFirstDayOfWeek(Calendar.MONDAY);
回答by Kariem
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. The system's default can be overridden by a JVM parameter:
一周的第一天源自当前语言环境。如果您不设置日历的语言环境(Calendar.getInstance(Locale)或new GregorianCalendar(Locale)),它将使用系统的默认值。系统的默认值可以被 JVM 参数覆盖:
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)
-Duser.language=en -Duser.country=US-> (周日)en_US: 1-Duser.language=en -Duser.country=GB-> (星期一)en_GB: 2
Don't forget that this could change other behavio(u)r too.
不要忘记这也可能会改变其他行为。
回答by toolkit
According to the API:
根据API:
Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a Calendar is constructed. They may also be specified explicitly through the methods for setting their values.
Calendar 使用两个参数定义了特定于语言环境的每周 7 天:一周的第一天和第一周的最少天数(从 1 到 7)。这些数字是在构建日历时从语言环境资源数据中获取的。它们也可以通过设置它们的值的方法明确指定。
So if you ensure that your locale is appropriately configured, this will be implicitly set. Personally, I would prefer explicitly setting this...
因此,如果您确保正确配置了您的语言环境,这将被隐式设置。就我个人而言,我更喜欢明确设置这个......
See #64038for ways to set a locale from the command line.
回答by ricafeal
Have you tried to invoke the JVM with a different locale? But you should be careful with side effects...
您是否尝试过使用不同的语言环境调用 JVM?但是你应该小心副作用......

