joda-time library pattern "d" not translating locale language

205 views Asked by At

I am using joda-time library for my Android project instead of Calendar. but unlike calendar I am facing a problem with locale language.

According to system/device language all type of text is translating but the "d" pattern is not translating, it always stays in English language.

Like say with "d" pattern, if a variable is 15, for English language locale it is: 15, but for Arabic language locale it is still: 15, it should convert to arabic 15 (١٥). It is not translating.

How can I do this? is this something joda-time lib does not support, or I am doing something wrong.

1

There are 1 answers

0
Arvind Kumar Avinash On

Check the following notice at the Home Page of Joda-Time

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Using the modern date-time API:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter dtfArabic = DateTimeFormatter.ISO_DATE.localizedBy(Locale.forLanguageTag("ar-SA"));
        System.out.println(date.format(dtfArabic));
    }
}

Output:

٢٠٢١-٠٢-٠٤

Learn more about the modern date-time API from Trail: Date Time.

For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.