How to set first day of week in a Java application calendar
Asked Answered
E

5

8

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

Edith answered 8/8, 2012 at 6:6 Comment(4)
Java/Swing doesn't have a Date picker control. What library is yours coming from?Ehling
I don't know, I'm only an end-user. I just want to change the first day of the week.Edith
Then I don't think you can control it (unless there's some configuration somewhere). You will need to contact the developersEhling
By the way: WeekFields.of​(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek)Alderete
K
3

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.
Karnak answered 8/8, 2012 at 6:19 Comment(2)
Can I do it from "Windows Control Panel > Java" without using any command line arguments?Edith
I never tried and I'm on linux. But on the tab "Java" show the runtime environment settings and try adding the "-Duser..." to the Runtime Parameter columnKarnak
I
27

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

Incogitable answered 8/8, 2012 at 6:18 Comment(4)
@Next Door Engineer what is the default starting day? MONDAY or SUNDAY?Swinger
Calendar.DAY_OF_WEEK is a constant value which doesn't change even if you change the first day of week. For example, 1 is always "sunday" and 6 always means "friday".Virgiliovirgin
@Virgiliovirgin So then what has changed? How do I get the day of the week that is correctly changed with after setting a new first day of the week?Martine
@RileyFitzpatrick WEEK_OF_MONTH and WEEK_OF_YEAR will change with setFirstDayOfWeek(). You will need to programmatically calculate it yourself if you want Monday=1 instead of Sunday=1.Virgiliovirgin
E
5

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"));
Enrage answered 17/1, 2018 at 6:53 Comment(1)
FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle.Alderete
K
3

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.
Karnak answered 8/8, 2012 at 6:19 Comment(2)
Can I do it from "Windows Control Panel > Java" without using any command line arguments?Edith
I never tried and I'm on linux. But on the tab "Java" show the runtime environment settings and try adding the "-Duser..." to the Runtime Parameter columnKarnak
S
1
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]);
Stricklin answered 8/8, 2012 at 6:42 Comment(0)
L
0

one words at short:

WeekFields.of(DayOfWeek.MONDAY, 1)

this is some example:

int weekOfYear1 = LocalDateTime.parse("2023-01-02 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        .get(WeekFields.SUNDAY_START.weekOfWeekBasedYear());
int weekOfYear2 = LocalDateTime.parse("2023-01-02 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        .get(WeekFields.ISO.weekOfWeekBasedYear());
int weekOfYear3 = LocalDateTime.parse("2023-01-02 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
        .get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfWeekBasedYear());
weekOfYear1 is 1
weekOfYear1 is 1
weekOfYear1 is 2
Linkage answered 20/2, 2023 at 9:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.