1

I want to get the name of the day in Arabic from Date using SimpleDateFormat and UmmalquraCalendar by applying the pattern.

Locale ar = new Locale("ar");
SimpleDateFormat dateFormat = new SimpleDateFormat("", ar );
Calendar hjCalender = new UmmalquraCalendar(ar);

dateFormat.setCalendar(hjCalender);
dateFormat.applyPattern("EEEE");
String day = dateFormat.format(hjCalender.getTime());
Log.v("MainActivity", "aa " + day);

it display aa so get empty String

[Update]

dateFormat = new SimpleDateFormat( "MMMM", new Locale("ar")) ; 
dateFormat.setCalendar(hjCalender);
Log.v("MainActivity" ," month ar " + 
dateFormat.format(hjCalender.getTime()));

and I got V/MainActivity: month ar ٠٠١ (not display name of month ).

Mahmoud Mabrok
  • 1,362
  • 16
  • 24
  • Weird. I tried the same with `Calendar.getInstance()` and got السبت. I would have expected you to get the same. – Ole V.V. Jul 07 '18 at 09:32
  • 2
    How are you viewing the string that was logged? Is it possible the unicode characters are getting lost because you're viewing it with Latin-1 or Cp1252 or something that isn't handling unicode correctly? – David Conrad Jul 07 '18 at 09:57
  • dateFormat = new SimpleDateFormat( "MMMM", new Locale("ar")) ; dateFormat.setCalendar(hjCalender); Log.v("MainActivity" ," month ar " + dateFormat.format(hjCalender.getTime()) ) ;
    and i got
    V/MainActivity: month ar ٠٠١
    – Mahmoud Mabrok Jul 07 '18 at 17:52

2 Answers2

1

EDIT: This answer is not recommended for older Android (below API level 26). While I expect it to give you the correct day of week always, it will only give a correct um Al-Qura calendar on Java 8 and above and on Android API level 26 and above.

The HijrahDate of java.time with default settings gives you the umm Al-Qura calendar:

    Locale ar = new Locale("ar");
    DateTimeFormatter dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEEE", ar);
    HijrahDate today = HijrahDate.now(ZoneId.of("Asia/Riyadh"));
    System.out.println("Chronology: " + today.getChronology());
    String day = today.format(dayOfWeekFormatter);
    System.out.println("aa " + day + " length " + day.length());

Running this snippet today (Hijrah-umalqura AH 1439-10-26) gives:

Chronology: Hijrah-umalqura
aa الثلاثاء length 8

I cannot tell why your code didn’t work. As I said in a comment, I could not reproduce your error. However, the SimpleDateFormat class that you are using has proved troublesome and is long outdated. So the idea is that if you are programming for Android API level 26 or higher, you throw away SimpleDateFormat and instead of ummalqura-calendar use the built-in java.time, the modern Java date and time API, and its HijrahDate.

I had first suggested that for API levels lower than 26 you could use ThreeTenABP, the Android adaption of ThreeTen Backport, the backport of java.time for Java 6 and 7. However, in comment Meno Hochschild informs us that this is not reliable:

Even if you have by accident found an example where threeten backport seems to return the expected result, it will not be the umalqura calendar. Many dates deviate by one day. Unfortunately, the backport has not copied the latest state of the Hijri calendar in java.time but still uses an older state of threeten development where an algorithmic version of Hijri calendar had been chosen. If you look at the javadoc of both libraries you will find a big difference, too. Here, the backport is no backport at all.

(Since comments on Stack Overflow are sometimes deleted, I quote this one here in its entirity.)

Yet another option on Android is to research whether the library by the same Meno Hochschild, Time4A, will give you what you want. It’s in the third link below. I have no experience with it myself, unfortunately.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    ThreetenABP (and ThreetenBP) does not offer the umalqura calendar! – Meno Hochschild Jul 07 '18 at 21:18
  • Thanks, @MenoHochschild, for your comment. As I said, I used ThreeTen Backport for the above snippet. It ran and gave the stated output. `HijrahDate` is in `threetenbp-1.3.6.jar`. Am I misinterpreting when it says it uses `Hijrah-umalqura` chronology?? – Ole V.V. Jul 08 '18 at 03:07
  • 1
    Even if you have by accident found an example where threeten backport seems to return the expected result, it will not be the umalqura calendar. Many dates deviate by one day. Unfortunately, the backport has not copied the latest state of the Hijri calendar in `java.time` but still uses an older state of threeten development where an algorithmic version of Hijri calendar had been chosen. If you look at the javadoc of both libraries you will find a big difference, too. Here, the backport is no backport at all. – Meno Hochschild Jul 09 '18 at 05:10
  • @MenoHochschild I have revised the answer after your comments. Thanks. – Ole V.V. Jul 10 '18 at 09:58
  • 1
    Well, I have now investigated the code given by OP and even downloaded the source code of the lib of msarhan but have not been able to reproduce the problem. All seems to work fine but maybe the Android environment modifies the behaviour of the code example?! About library alternatives: There is also ICU4J which offers an [umalqura calendar](https://developer.android.com/reference/android/icu/util/IslamicCalendar) since API level 24 (and 3 other variants configurable). FYI, `java.time` only offers one variant (umalqura). My lib Time4A offers 11 variants. – Meno Hochschild Jul 10 '18 at 17:26
0

You could achieve the result using DateFormatSymbols.

Date date = new Date(timestamp); // date for which you want to know the day 

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);//This will return the day of week
    DateFormatSymbols symbols = new DateFormatSymbols(new Locale("ar"));
    String[] dayNames = symbols.getWeekdays();//will get you name of days in arabic
    return dayNames[dayOfWeek];
Shivam Pokhriyal
  • 1,044
  • 11
  • 26