我正试图展示荷兰月份。但这个月用英文打印出来。这需要在Android API 19及更高版本中运行。
compile 'joda-time:joda-time:2.9.9'
val test = DateTime()
val l = Locale("nl_NL") // Dutch language, Netherlands country.
val f = DateTimeFormat.forPattern("dd MMM yyyy").withLocale(l)
val text = f.print(test)
打印出来:
2017年10月26日
应该是:
2017年5月20日
答案 0 :(得分:2)
您必须使用Locale
' 2-arg constructor,它们会在不同的参数中接收语言和国家/地区代码:
val l = Locale("nl", "NL")
这样,输出为:
26 okt 2017
在我的测试中,输出不是你想要的大写Okt
,但是它内置在API中,我们无法控制它。如果您想要Okt
作为输出,则您必须自己操纵字符串。
答案 1 :(得分:1)
正确答案:
val l = Locale("nl", "NL")
val f = DateTimeFormat.forPattern("dd MMM yyyy").withLocale(l)
val dateStr = f.print(dateTime).substring(3, 4).toUpperCase()
val capitalizedDate = StringBuilder(f.print(dateTime))
capitalizedDate.setCharAt(3, dateStr[0])
return capitalizedDate.toString().replace(".", "")
答案 2 :(得分:0)
我已经回答了类似的问题here。您可以使用Calendar
对象获取本地化的月份名称。
private String getMonthName(final int index, final Locale locale, final boolean shortName)
{
String format = "%tB";
if (shortName)
format = "%tb";
Calendar calendar = Calendar.getInstance(locale);
calendar.set(Calendar.MONTH, index);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return String.format(locale, format, calendar);
}
完整月份名称的示例:
System.out.println(getMonthName(0, new Locale("NL"), false));
结果:januari
短月名称示例:
System.out.println(getMonthName(2, new Locale("NL"), true));
结果:jan.