Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify firstDayOfWeek for java.util.Calendar using a JVM argument

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);
like image 325
user35172 Avatar asked Nov 06 '08 17:11

user35172


People also ask

What is calendar getInstance () in Java?

The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.

What is calendar Day_of_year in Java?

Tag: DAY_OF_YEAR We can define a calendar for a specific day of the year by setting the java. util. Calendar object DAY_OF_YEAR field using the set() method. The method take the field to be set and a value.


2 Answers

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

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

  • -Duser.language=en -Duser.country=US -> en_US: 1 (Sunday)
  • -Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)

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

like image 197
Kariem Avatar answered Nov 15 '22 18:11

Kariem


According to the 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.

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.

like image 25
toolkit Avatar answered Nov 15 '22 18:11

toolkit