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

查看:22
本文介绍了在 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".

首先,创建的自定义实现日历数据提供者:

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/foad/utils/ArabicCalendarDataProvider.class

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

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

io.foad.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,HOST

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

SPI 应该是第一个.

您可以按如下方式进行测试:

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 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天全站免登陆