1

I'm trying to show dutch months. But the month is printed out in English. This needs to work from Android API 19 and higher.

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)

Prints out:

26 Oct 2017

Should be:

26 Okt 2017

Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94

3 Answers3

2

You must use the Locale's 2-arg constructor, that receives the language and country code in separate parameters:

val l = Locale("nl", "NL") 

With this, the output is:

26 okt 2017

In my tests, the output is not in uppercase Okt as you wanted, but that's built-in in the API and we have no control over it. If you want Okt as output, you'll have to manipulate the string by yourself.

1

Correct answer:

    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(".", "")
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
0

I already answered to a similar question here. You can get a localized month name using Calendar object.

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);
}

Example for full month name:

System.out.println(getMonthName(0, new Locale("NL"), false));

Result: januari

Example for short month name:

System.out.println(getMonthName(2, new Locale("NL"), true));

Result: jan.

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56