Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arabic Date on Android App

Tags:

java

date

android

I'm working on android app , and the next release to add Arabic language on the app , but i have a problem .

this problem is : the Android OS convert the date dynamically to Arabic format , and i used it in URL parameter, and the server can't read it .

How can i convert any Arabic date to English date ?!?

what the Android OS show me : ١٤-٠٥-٢٠١٤
What i need is : 2014-05-14

I tired some Java lines like this :

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);

And Android doesn't has the Locale.Arabic format

The problem appears when you convert your phone into Arabic Language

like image 213
Muhamet Aljobairi Avatar asked May 12 '14 14:05

Muhamet Aljobairi


People also ask

How do I put Arabic date on my phone?

Go to Settings. Tap “Calendar” Tap “Alternate Calendars” Select “Islamic”

How can I set Islamic date in Android?

Click on Settings and under the General tab, choose Alternate Calendars and select the Hijri calendar of your choice.

How do I put the date on my Islamic screen?

Go on "Settings" --> Scroll down --> Click on. "Calendar" --> Click on "Alternate Calendars" --> Select "Islamic" option from the list. Feel free to share it with others.

How do I change my Hijri date on Google?

Select Settings. In the Date/time formatting settings section of the Settings overlay, use the drop-downs to select your preferences. Use the Dates drop-down to set how dates appear.


2 Answers

tl;dr

Your desired format happens to be defined in the ISO 8601 standard. The java.time classes use the standard formats by default when parsing/generating text.

LocalDate.now().toString()

2019-01-23

To localize to English in the United States:

LocalDate
.now(
    ZoneId.of( "America/Chicago" ) 
)
.format(
    DateTimeFormatter.ofLocalizedDate(
        FormatStyle.FULL   // or LONG or MEDIUM or SHORT
    )
    .withLocale(  
        new Locale( "en" , "US" )   // English language, with cultural norms of United States. 
    )
)

For Arabic in Tunisia, use:

new Locale( "ar" , "TN" )

Avoid legacy date-time classes

You are using the terrible date-time classes bundled with the earliest versions of Java. They were supplanted entirely by the java.time classes with the adoption of JSR 310. For earlier Android, see bullets at bottom below.

Locale

And Android doesn't has the Locale.Arabic format

Only a few of the many locales have a named constant. Learn to specify a Locale by the standard language code and country (culture) code, as explained in the Locale class. For more info, see this related Question.

For example, for Arabic language with Saudi Arabia cultural norms, use:

Locale locale = new Locale( "ar" , "SA" ) ;  // Arabic language. Saudi Arabia cultural norms.

We can use that locale to localize the text representing a moment.

ZoneId z = ZoneId.of( "Pacific/Auckland" );  // By the way, time zone has *nothing* to do with locale, orthogonal issues.
ZonedDateTime zdt = ZonedDateTime.now( z );
Locale locale = new Locale( "ar" , "SA" );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );

When run.

الجمعة، 18 يناير 2019 12:14:00 م توقيت نيوزيلندا الصيفي

To see all the locale definitions available on your system, run this code.

    for ( Locale locale : Locale.getAvailableLocales() ) {
        System.out.println( locale.toString() + "  Name: " + locale.getDisplayName( Locale.US ) );
    }

Today's date in English

The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

Or, better, use the Month enum objects pre-defined, one for each month of the year. Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto for Year & YearMonth.

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

To generate text localized to English, use DateTimeFormatter.ofLocalizedDate.

FormatStyle formatStyle = FormatStyle.FULL ;
Locale locale = new Locale( "en" , "US" ) ; // English language, United States cultural norms.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( formatStyle ).withLocale( locale );
String output = localDate.format( f ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 200
Basil Bourque Avatar answered Oct 20 '22 12:10

Basil Bourque


This is working

val formatterNew = SimpleDateFormat(requiredFormat,Locale.forLanguageTag("ar"))

like image 30
Sumit Ojha Avatar answered Oct 20 '22 12:10

Sumit Ojha