我需要返回Date对象之外的阿拉伯/法国月份名称

问题描述 投票:2回答:4

我正在处理一个应用程序,我需要返回阿拉伯语/法语的月份名称和日期名称,具体取决于我使用的本地语言。例如,如果当地人是“ Ar”,则我需要将月份作为2月的فبراير。如果本地为“ Fr”,则月份名称应为Février。我目前正在解决此问题,并从不同的.properties文件读取月和日名称。现在,我的问题是:有没有办法根据给定的本地语言以不同的语言返回月份和日期的名称?

非常感谢您的帮助:)

java spring internationalization
4个回答
7
投票

您不必解决它。使用SimpleDateFormat(format, locale)。这是代码示例:

    SimpleDateFormat fen = new SimpleDateFormat("dd/MMMM/yyyy", new Locale("en"));
    SimpleDateFormat ffr = new SimpleDateFormat("dd/MMMM/yyyy", new Locale("fr"));

    Date date = new Date();

    System.out.println(fen.format(date));
    System.out.println(ffr.format(date));

4
投票

格式化程序是最灵活的变体。有方便的方法,例如String.format()和printf()也在使用Formatter。

Date d = new Date();

Locale saudi = new Locale("ar","SA");

Formatter formatter = new Formatter(System.out,Locale.FRENCH);
formatter.format("month: %tB\n", d);
System.out.printf(saudi, "month: %tB\n", d);

月:décembre月:ديسمبر


2
投票

使用getMonths类的getWeekdaysDateFormatSymbols方法

DateFormatSymbols

0
投票

尝试这种方法:传递format的值,您将以这种格式传递JAN的月份名称参数,例如“ MMM”。

String getMonthName(int month, Locale locale) {
    return DateFormatSymbols.getInstance(locale).getMonths()[month];
}
© www.soinside.com 2019 - 2024. All rights reserved.