1

I'm using a JFXDatePicker (material design DatePicker) in JavaFX and I want to change the starting day of the week in the calendar that pops-up when I click the DatePicker. Currently, the starting day of the week is Sunday (S M T W T F S), but I want to change it to Monday (M T W T F S S).

Is this possible? I made a small research, but I had no success.

Thank you!

Alex
  • 149
  • 1
  • 1
  • 7
  • This is what you are looking for: https://confluence.atlassian.com/jirakb/how-to-set-monday-as-first-day-of-week-in-date-picker-calendar-604209836.html Or this: https://stackoverflow.com/questions/29168403/set-the-first-day-of-the-week-in-javafxs-datepicker – Jens van Groeningen Feb 26 '21 at 09:29
  • afaik, changing just the firstDayOfWeek is not supported (at least not on a plain DatePicker, don't know about JFXDatePicker, though I suspect it doesn't) - all locale-sensitive state of the DatePicker is controlled by the default Locale (see DatePickerContent on how it is done). – kleopatra Feb 26 '21 at 11:20
  • 1
    Did you try to change the locale of your jvm at start-up? – Puce Feb 26 '21 at 12:29
  • @Puce no, I didn't. I will try. Thanks! – Alex Feb 26 '21 at 13:18
  • @kleopatra I will check DatePickerContent. Thanks! – Alex Feb 26 '21 at 13:19
  • 1
    Maybe this can be solved using @kleopatra's solution, but I managed to solve it just by adding these JVM parameters (for my country, Romania): -Duser.country=RO -Duser.language=ro – Alex Feb 26 '21 at 13:39
  • 1
    wondering why that's not the default Locale? – kleopatra Feb 26 '21 at 16:04

1 Answers1

4

DatePicker doesn't support setting of firstDayOfWeek. It (or better: its kin, which is using DatePickerContent to present the day grid) takes that property from the application default Locale.

To force a deviation of the default firstDayOfWeek for any given Locale, we can build a new Locale from the default and set its new value via an extension:

public static Locale adjustWeekStart(Locale locale, DayOfWeek day) {
    String dayString = day.toString().substring(0, 3);
    Locale weekStart = new Locale.Builder()
            .setLocale(locale)
            .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "fw-" + dayString)
            .build();
    
    return weekStart;
}

Note: the key "fw" is specified in WeekFields.of(Locale), the value being the first three letters of the day name is taken from the implementation of CalendarDataUtility (please edit if that's specified somewhere ;)

Now we can use that modified Locale as default and get DatePickers with a modified firstDayOfWeek:

public class DatePickerExperiments extends Application  {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Locale.setDefault(adjustWeekStart(Locale.getDefault(), DayOfWeek.SATURDAY));
        
        DatePicker datePicker = new DatePicker();
        Scene scene = new Scene(new HBox(datePicker), 300, 240);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211