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

2022-01-11 00:00:00 calendar java jvm-arguments

我正在尝试将 java.util.Calendar 的默认 firstDayOfWeek 从 SUNDAY 更改为 MONDAY.是否可以通过JVM配置而不是添加这段代码来实现?

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?

cal.setFirstDayOfWeek(Calendar.MONDAY);

推荐答案

一周的第一天派生自当前语言环境.如果您没有设置日历的区域设置 (Calendar.getInstance(Locale),或 new GregorianCalendar(Locale)),它将使用系统的默认值.系统的默认值可以被 JVM 参数覆盖:

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:

public static void main(String[] args) {
    Calendar c = new GregorianCalendar();
    System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek());
}

这应该显示具有不同 JVM 参数的不同输出:

This should show a different output with different JVM parameters for language/country:

  • -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.

相关文章