問題描述
我正在嘗試將 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.
這篇關于如何使用 JVM 參數為 java.util.Calendar 指定 firstDayOfWeek的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!