适当的俄语月字符串翻译Java

abh*_*hig 12 java localization date

我想将日期转换为俄语并使用下面的代码

SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG,locale).format(date);
Run Code Online (Sandbox Code Playgroud)

locale是Locale类型的地方问题是未正确解析几个月.1月即将出现"январь"它应该是"января"而2月即将到来,因为"февраль"应为"февраля"

等等...

一个想法是在我的逻辑中将不正确的月份转换为正确的月份

Java有没有自动执行此操作?

谢谢

Men*_*ild 22

在我的JDK-6安装上,我可以重现你的问题:

Date jud = new SimpleDateFormat("yyyy-MM-dd").parse("2014-02-28");
String month =
    DateFormat.getDateInstance(SimpleDateFormat.LONG, new Locale("ru")).format(jud);
System.out.println(month); // output: 28 ??????? 2014 ?.
Run Code Online (Sandbox Code Playgroud)

Java-8为您提供解决方案.

似乎JDK已将内部默认值从"独立式"(主格式)更改为"格式式"(genitive).

String date =
  DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
  .withLocale(new Locale("ru"))
  .format(LocalDate.of(2014, 2, 28));
System.out.println(date); // output: 28 ??????? 2014 ?.
Run Code Online (Sandbox Code Playgroud)

如果你需要应用独立的textstyle,那么你必须设置自己的DateTimeFormatterBuilder,这需要更多的努力,否则TextStyle.FULL应该是默认的.

String m = Month.FEBRUARY.getDisplayName(TextStyle.FULL , new Locale("ru")); 
// ??????? (first and last char are different)

String s = Month.FEBRUARY.getDisplayName(TextStyle.FULL_STANDALONE , new Locale("ru")); 
// ??????? (this style can be used in DateTimeFormatterBuilder for the month field, too)
Run Code Online (Sandbox Code Playgroud)

使用旧样式的Java-pre-8的解决方法:

定义自己的文本资源(麻烦)!

Locale russian = new Locale("ru");
String[] newMonths = {
  "??????", "???????", "?????", "??????", "???", "????", 
  "????", "???????", "????????", "???????", "??????", "???????"};
DateFormatSymbols dfs = DateFormatSymbols.getInstance(russian);
dfs.setMonths(newMonths);
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, russian);
SimpleDateFormat sdf = (SimpleDateFormat) df;
sdf.setDateFormatSymbols(dfs);

Date jud = new SimpleDateFormat("yyyy-MM-dd").parse("2014-02-28");
String month = sdf.format(jud);
System.out.println(month); // output: 28 ??????? 2014 ?.
Run Code Online (Sandbox Code Playgroud)

Joda-Time在Java-pre-8环境中不提供良好的解决方案,因为它只委托给JDK.另请参阅Joda-site上的类似问题.

最后还有我的库Time4J可以解决像Java-8这样的问题,但是它使用自己的俄语文本资源并理解两种形式(旧样式和独立样式),所以这是旧Java版本的简单解决方案(由于许多其他功能增强,当然不会被Java-8淘汰).

System.out.println(
    ChronoFormatter.ofDateStyle(DisplayMode.FULL, new Locale("ru")).format(
        PlainDate.of(2014, Month.FEBRUARY, 28)
    )
); // output: 28 ??????? 2014 ?.
Run Code Online (Sandbox Code Playgroud)


Vit*_*iev 13

对于Java 8,您可以使用新模式.

简而言之:该"LLLL"模式将获得一个有名的案例:

new SimpleDateFormat("LLLL", Locale.getDefault()).format(date); // ??????
Run Code Online (Sandbox Code Playgroud)

"MMMM"模式将返回String所有格情况:

new SimpleDateFormat("MMMM", Locale.getDefault()).format(date); // ??????
Run Code Online (Sandbox Code Playgroud)

或者,不是硬编码俄罗斯月份(因为我们有波兰语,乌克兰语和其他语言),你可以使用java.time.Month枚举.它包含月份int编号和String名称.


Coo*_*ind 5

虽然@Meno Hochschild和/sf/answers/1919477241/的正确答案是正确的,但我想补充一点。

设置Locale("ru"),创建和应用就足够了sdf.format(date)

public static String formatDate(long date, String format) {
    Locale locale = new Locale("ru");
    SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
    return sdf.format(date);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果要自定义它,我将显示一个过程。

经过许多例外之后,我意识到工作日并非从星期一开始(请参阅http://jexp.ru/index.php/Java_Tutorial/Data_Type/Date_Format#Change_date_formatting_symbols)!

public static String formatDate(long date, String format) {
    //Locale locale = new Locale("fr");
    Locale locale = new Locale("ru");
    DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
    String[] months = {
            "??????", "???????", "?????", "??????", "???", "????",
            "????", "???????", "????????", "???????", "??????", "???????"};
    String[] shortMonths = {
            "???", "???", "???", "???", "???", "???",
            "???", "???", "???", "???", "???", "???"};
    dfs.setMonths(months);
    dfs.setShortMonths(shortMonths);
    String[] weekdays = {"", "???????????", "???????????", "???????", "?????", "???????", "???????", "???????"};
    String[] shortWeekdays = {"", "??", "??", "??", "??", "??", "??", "??"};
    dfs.setWeekdays(weekdays);
    dfs.setShortWeekdays(shortWeekdays);

    SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
    sdf.setDateFormatSymbols(dfs);
    return sdf.format(date); // ??, 09 ??????? 2016
}
Run Code Online (Sandbox Code Playgroud)