Search code examples
javainternationalizationjodatimeicu

Can Joda format dates for Arabic locale using Arabic digits?


The story so far...

We got sick of Java's Date API being generally bad and eventually ended up adopting Joda Time, which works much more sanely (although it is not perfect.)

Recently we have been noticing issues in Java's NumberFormat with respect to Arabic locale. Essentially, it uses the "latin" digits instead of the more natural "arab" digits. So I have been adopting ICU's formatters as drop-in replacements for Java's.

Now I have noticed that Joda's formatters for dates print the numbers in Latin as well. For example, if you take the following program:

import java.util.Date;
import java.util.Locale;

import com.ibm.icu.text.DateFormat;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

public class TestJoda {
    @Test
    public void test() {
        Locale locale = new Locale("ar", "SA");

        DateFormat icuDateFormat = DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG, locale);
        System.out.println("ICU: " + icuDateFormat.format(new Date()));

        DateTimeFormatter jodaDateTimeFormatter =
            DateTimeFormat.longDateTime().withLocale(locale);
        System.out.println("Joda: " + jodaDateTimeFormatter.print(DateTime.now()));
    }
}

The output will be as follows:

ICU: ٤ أغسطس، ٢٠١٤ ٥:٠٧:١٤ م جرينتش+١٠
Joda: 04 أغسطس, 2014 EST 05:07:14 م

Is there some way to coerce Joda's formatters into outputting a style consistent to ICU's?

I guess in the worst case there would be a way to implement some kind of adapter, but one of the nice things about Joda's classes is their immutability, a feature which ICU has unfortunately been slow to adopt.


Solution

  • Joda only supports ASCII digits, it's hardcoded.

    You can always use String.replace, and/or wrap DateTimeFormatter into a proxy.