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

I want to convert a Date in to Russian and using the code below

SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG,locale).format(date);

where locale is of type Locale

The problem is months are not parsed correctly . January is coming as "январь" it should be "января" and February is coming as "февраль" should be "февраля"

and so on...

One idea is to convert incorrect months to proper ones in my logic

Is there any thing by which Java do this automatically ?

Thanks

解决方案

On my JDK-6-installation I can reproduce your problem:

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 г.

Java-8 offers you a solution.

It seems that the JDK has changed the internal default from "standalone-style" (nominative) to "format-style" (genitive).

String date =

DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)

.withLocale(new Locale("ru"))

.format(LocalDate.of(2014, 2, 28));

System.out.println(date); // output: 28 февраля 2014 г.

If you need to apply standalone textstyle then you have to set up your own DateTimeFormatterBuilder which requires a little bit more effort, else TextStyle.FULL should be the default.

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)

Workaround for Java-pre-8 using old style:

Define your own text resources (troublesome)!

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 г.

Joda-Time does not offer a good solution in a Java-pre-8 environment because it only delegates to JDK. See also a similar issue on Joda-site.

Finally there is also my library

System.out.println(

ChronoFormatter.ofDateStyle(DisplayMode.FULL, new Locale("ru")).format(

PlainDate.of(2014, Month.FEBRUARY, 28)

)

); // output: 28 февраля 2014 г.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值