tatarusanu1
tatarusanu1

Reputation: 67

Joda Time bad formatting when using locales other than english

I'm using Joda Time 2.9.9 in Android. When the Locale is set to english, calls like datetime.monthOfYear().getAsText(locale) and datetime.monthOfYear().getAsShortText(locale) return strings formatted like February and Feb respectively.

But when the locale is to to spanish (es) it returns strings such as february and feb. (actual values are in spanish, I translated them).

Is there any way to make it return nice formatted strings, capitalized and without dots, when using other locales?

And a bonus question: If there is no way, is subclassing the datetime class and formatting the string there a good idea?

Upvotes: 1

Views: 347

Answers (1)

jmj
jmj

Reputation: 11

Joda-Time gets this info from the JVM's DateFormatSymbols and I'm not sure if those can be replaced via API (probably you can do it using reflection, but I personally don't like to do these things in production code).

Anyway, I think you can adapt the solution found in this question: Month name in genitive (Polish locale) with Joda-Time DateTimeFormatter

PS: I can't reproduce the error, the code below:

DateTime datetime = new DateTime();
System.out.println(datetime.monthOfYear().getAsText(new Locale("es")));
System.out.println(datetime.monthOfYear().getAsShortText(new Locale("es")));

gives me "febrero" and "feb"

Upvotes: 1

Related Questions