How to set first day of week in a Java application calendar
Question
We use a java application, it has a date selection field, when you click there a small calendar opens. First day of the week is Sunday there. But I want it to be Monday. I try to change it from Windows Control Panel from Date settings. I set Windows calendar's first day to Thursday, for instance. But in Java application's calendar, first day of the week is still Sunday. Is it possible to change the Java application's first day of the week from Windows, or is it only changed from Java application's code?
Regards
Solution
Which framework does your java app use? What kind of component is the date selection field?
In Java's Calendar
the first day of week by default is determined by the Locale setting of your system.
So if you cannot change the source code of your application:
- you might want to change the locale of your operating system (in your case Windows)
- or you might use various command line arguments like
-Duser.country
or-Duser.region for
java when firing up your jvm. Have a look here.
OTHER TIPS
You can use the method setFirstDayOfWeek() to set the first day of the week. The method can only affect the return values of WEEK_OF_MONTH or WEEK_OF_YEAR. For DAY_OF_WEEK, it does nothing.
You can implement something like:
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int rec = cal.get(Calendar.WEEK_OF_MONTH);
System.out.println(rec);
Read more on the API HERE
If you want to set Monday then use
Calendar currentCalendar = Calendar.getInstance(new Locale("en","UK"));
If you want to set Sunday then use
Calendar currentCalendar = Calendar.getInstance(new Locale("en","US"));
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
String[] strDays = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday",
"Friday", "Saturday" };
// Day_OF_WEEK starts from 1 while array index starts from 0
System.out.println("Current day is : " + strDays[now.get(Calendar.DAY_OF_WEEK) - 1]);