在JavaFX的DatePicker中设置一周的第一天 [英] Set the first day of the week in JavaFX's DatePicker

查看:106
本文介绍了在JavaFX的DatePicker中设置一周的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用阿拉伯语言环境时,是否可以在JavaFX的DatePicker上更改一周的第一天?我需要将其从周六更改为周日.

Is it possible to change the first day of week on JavaFX's DatePicker when applying Arabic locale? I need to change it from Saturday to Sunday.

推荐答案

通过在应用程序中注入以下类,我可以更改一周的第一天:

I was able to change the first date of the week by injecting the following class within 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" } };
    }
}

不要忘记更改默认语言环境:

Do not forget 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);


以前的解决方案不适用于Java 8(u152)的最新更新.实现此目标的正确方法是使用区域设置敏感服务提供商".


The previous solution does not work on latest updates of Java 8 (u152). The proper way to achieve that is to use something called "Locale Sensitive Service Provider".

首先,创建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());
    }
}

然后创建一个jar文件,并将 ArabicCalendarDataProvider 打包为服务提供者.即jar文件将包含以下文件:

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

META-INF/services/java.util.spi.CalendarDataProviderio/fouad/utils/ArabicCalendarDataProvider.class

META-INF/services/java.util.spi.CalendarDataProvider io/fouad/utils/ArabicCalendarDataProvider.class

文件 java.util.spi.CalendarDataProvider 包含以下行:

io.fouad.utils.ArabicCalendarDataProvider

io.fouad.utils.ArabicCalendarDataProvider

现在,为了使其正常运行,您需要将该jar作为扩展安装,方法是将该jar放入默认扩展目录中,或者在启动时传递以下JVM参数:

Now, in order to make it working you need to install this jar as an extension either by putting the jar into the default extension directory or by passing the following JVM argument at launch time:

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

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

请注意,在Java 9中,如果jar位于应用程序的类路径中,则对区域设置敏感的服务实现将直接起作用.

Note that in Java 9 the locale sensitive services implementations will work directly if the jar is on the application's classpath.

最后,您需要修改应用程序中使用的语言环境提供程序的顺序.即,传递以下JVM参数:

Last, you need to modify the order of locale providers used in your application. i.e. pass the following JVM argument:

-Djava.locale.providers = SPI,CLDR,JRE,主机

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

SPI 应该是第一个.

您可以如下进行测试:

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

在默认行为下,输出为:

In the default behavior the output would be:

firstDayOfWeek =星期六

firstDayOfWeek = SATURDAY

应用自定义区域设置提供程序并将 SPI 放在第一位时,输出为:

When applying the custom locale provider and put SPI as first one the output would be:

firstDayOfWeek =星期天

firstDayOfWeek = SUNDAY

请参阅:

这篇关于在JavaFX的DatePicker中设置一周的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆