如何使用JVM参数为java.util.Calendar指定firstDayOfWeek

时间:2022-05-18 17:20:13

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);

3 个解决方案

#1


16  

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)或新的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 = US - > en_US:1(星期日)

  • -Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)
  • -Duser.language = en -Duser.country = GB - > en_GB:2(星期一)

Don't forget that this could change other behavio(u)r too.

不要忘记这可能会改变其他行为(你)。

#2


3  

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.

日历使用两个参数定义特定于语言环境的七天工作周:一周的第一天和第一周的最小天数(从1到7)。构建Calendar时,这些数字取自语言环境资源数据。它们也可以通过设置其值的方法明确指定。

So if you ensure that your locale is appropriately configured, this will be implicitly set. Personally, I would prefer explicitly setting this...

因此,如果确保已正确配置了区域设置,则将隐式设置此区域设置。就个人而言,我更愿意明确地设置这个......

See #64038 for ways to set a locale from the command line.

有关从命令行设置区域设置的方法,请参阅#64038。

#3


0  

Have you tried to invoke the JVM with a different locale? But you should be careful with side effects...

您是否尝试使用其他语言环境调用JVM?但你应该小心副作用......

#1


16  

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)或新的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 = US - > en_US:1(星期日)

  • -Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)
  • -Duser.language = en -Duser.country = GB - > en_GB:2(星期一)

Don't forget that this could change other behavio(u)r too.

不要忘记这可能会改变其他行为(你)。

#2


3  

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.

日历使用两个参数定义特定于语言环境的七天工作周:一周的第一天和第一周的最小天数(从1到7)。构建Calendar时,这些数字取自语言环境资源数据。它们也可以通过设置其值的方法明确指定。

So if you ensure that your locale is appropriately configured, this will be implicitly set. Personally, I would prefer explicitly setting this...

因此,如果确保已正确配置了区域设置,则将隐式设置此区域设置。就个人而言,我更愿意明确地设置这个......

See #64038 for ways to set a locale from the command line.

有关从命令行设置区域设置的方法,请参阅#64038。

#3


0  

Have you tried to invoke the JVM with a different locale? But you should be careful with side effects...

您是否尝试使用其他语言环境调用JVM?但你应该小心副作用......