Sana'a Ala'a
Sana'a Ala'a

Reputation: 173

How to convert date from English to Arabic

I have this code

frame.sigdate.setText(new SimpleDateFormat("yyyy/M/d").format(new Date()));

which reads the date from my PC with English numbers. What I want to do is convert the date to Arabic numbers. Is there anything like Local.ar ? I appreciate any help.

Upvotes: 3

Views: 3573

Answers (4)

Abrar Hussain
Abrar Hussain

Reputation: 35

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");

String date = "16/08/2011";
Locale arabicLocale = Locale.forLanguageTag("ar-SA");
DateTimeFormatter arabicDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(arabicLocale).withDecimalStyle(DecimalStyle.of(arabicLocale));

LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
today = LocalDate.parse(date, formatter);

String dat = today.format(arabicDateFormatter);
System.out.println(dat);

out put ١٦‏/٨‏/٢٠١١

Upvotes: 1

Anonymous
Anonymous

Reputation: 86276

java.time

    Locale arabicLocale = Locale.forLanguageTag("ar");
    DateTimeFormatter arabicDateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                    .withLocale(arabicLocale)
                    .withDecimalStyle(DecimalStyle.of(arabicLocale));
    LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
    System.out.println(today.format(arabicDateFormatter));

Output:

١٥‏/٤‏/٢٠١٨

The key is withDecimalStyle. Without this call, the formatter would still use western numerals, as in 15‏/4‏/2018. You may want to use a more specific language tag than just ar for Arabic, for example ar-BH for Bahrain or ar-YE for Yemen. See the link at the bottom for possibilities. You should also insert your desired time zone where I put Asia/Muscat.

EDIT: The above has been tested in Java 9. Surprisingly in Java 8 it still uses western (unlocalized) digits. A possible fix (or workaround if you like) is to specify the zero digit explicitly — it will pick up the other digits from it.

    DecimalStyle arabicDecimalStyle
            = DecimalStyle.of(arabicLocale).withZeroDigit('٠');
    DateTimeFormatter arabicDateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                    .withLocale(arabicLocale)
                    .withDecimalStyle(arabicDecimalStyle);

It’s an Arabic zero between the two apostrophes in the argument to withZeroDigit. Now I get this output on Java 8:

١٥/٠٤/١٨

It’s usually a good idea to use the built-in locale specific formats as I do with ofLocalizedDate in both snippets above. If you need finer control over the format, use ofPattern instead. For example, to get yyyy/mm/dd format:

    DateTimeFormatter arabicDateFormatter
            = DateTimeFormatter.ofPattern("uuuu/MM/dd", arabicLocale)
                    .withDecimalStyle(arabicDecimalStyle);

Output:

٢٠١٨/٠٤/١٥

The reason why the format changed from Java 8 to Java 9 is that Java has changed the defaults for where the locale data come from, including the built-in localized date and time formats. You can get the Java 9 format already in Java 8 by setting a system property, for example like this:

    System.setProperty("java.locale.providers", "CLDR,JRE,SPI");

With this change the first code snippet above gives the same output on Java 8 as on Java 9:

١٥‏/٤‏/٢٠١٨

The important detail here is that CLDR goes first in the property string. And the advantages are you don’t need to specify your own format pattern string, localization to other locales is straightforward and users won’t be surprised by a change in behaviour once you switch to Java 9 or later.

I am using and recommending java.time, the modern Java date and time API. The SimpleDateFormat class that you used in the question is not only long outdated, it is also notoriously troublesome. IMHO you should avoid it completely. The modern API is so much nicer to work with.

Links

Upvotes: 7

N00b Pr0grammer
N00b Pr0grammer

Reputation: 4647

DateTime dateTimeObjectInUTC = new DateTime(DateTimeZone.UTC);
DateTimeZone dateTimeZoneObject = DateTimeZone.forID("Asia/Riyadh");

java.util.Locale locale = new Locale("ar","SA");
DateTimeFormatter formatter = DateTimeFormat.forStyle("FF").withLocale(locale).withZone(dateTimeZoneObject);
String output = formatter.print(dateTimeObjectInUTC);

This should help!

I am using Joda-Time. Please refer to the Jodatime documentation. DateTimeZone documentation, for example.

Upvotes: 0

ssaa
ssaa

Reputation: 1112

try below approch

java.util.Locale locale = new java.util.Locale("ar");
java.text.DecimalFormat df = (java.text.DecimalFormat)
java.text.DecimalFormat.getNumberInstance(locale);

Upvotes: 0

Related Questions