Set the first day of the week in JavaFX DatePicker

Is it possible to change the first day of the week to JavaFX DatePicker when using Arabic? I need to change it from Saturday to Sunday.

enter image description here

+5
source share
1 answer

I managed to change the first date of the week by introducing the following class in my application:

package sun.util.resources.ar; import sun.util.resources.LocaleNamesBundle; public final class CalendarData_ar_SA extends LocaleNamesBundle { protected final Object[][] getContents() { return new Object[][] { { "firstDayOfWeek", "1" }, { "minimalDaysInFirstWeek", "1" } }; } } 

enter image description here


Remember to change the default locale:

 public static final Locale SAUDI_AR_LOCALE = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build(); // nu is for numbers // .... Locale.setDefault(SAUDI_AR_LOCALE); 

EDIT:

The previous solution does not work with the latest Java 8 (u152) updates. The right way to achieve this is to use something called the "Locale Sensitive Service Provider".

First create a custom implementation of CalendarDataProvider :

 package io.fouad.utils; import java.time.DayOfWeek; import java.util.Locale; import java.util.spi.CalendarDataProvider; public class ArabicCalendarDataProvider extends CalendarDataProvider { private static final DayOfWeek FIRST_DAY_OF_WEEK = DayOfWeek.SUNDAY; @Override public int getFirstDayOfWeek(Locale locale) { return (FIRST_DAY_OF_WEEK.getValue() + 1) % 7; } @Override public int getMinimalDaysInFirstWeek(Locale locale) { return 1; } @Override public Locale[] getAvailableLocales() { return new Locale[]{new Locale("ar", "SA")}; } @Override public boolean isSupportedLocale(Locale locale) { return locale != null && "ar".equals(locale.getLanguage()) && "SA".equals(locale.getCountry()); } } 

Then create the jar file and the ArabicCalendarDataProvider package as a service provider. those. The jar file will have the following files:

META-INF / services / java.util.spi.CalendarDataProvider I.O. / Fuad / Utils / ArabicCalendarDataProvider.class

The java.util.spi.CalendarDataProvider file contains the following line:

io.fouad.utils.ArabicCalendarDataProvider

Now, to make it work, you need to install this jar as an extension, either by putting the jar in the default extension directory, or by passing the following JVM argument at startup:

-Djava.ext.dirs = path / to // folder / that / contains / s / jar

Note that in Java 9, locale-dependent implementation services will work directly if the jar is in the class path of the application.

Finally, you need to reorder the locale providers used in your application. that is, pass the following JVM argument:

-Djava.locale.providers = SPI, CLDR, JRE, HOST

SPI should be the first.

You can test it as follows:

 DayOfWeek firstDayOfWeek = WeekFields.of(new Locale("ar", "SA")).getFirstDayOfWeek(); System.out.println("firstDayOfWeek = " + firstDayOfWeek); 

In the default behavior, the output will be as follows:

firstDayOfWeek = SATURDAY

When applying a custom locale provider and set the SPI as the first, the output will be:

firstDayOfWeek = SUNDAY

Cm:

+4
source

Source: https://habr.com/ru/post/1215753/


All Articles