Reputation: 1482
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 ).
Upvotes: 1
Views: 608
Reputation: 1094
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];
Upvotes: 0
Reputation: 86232
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.
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 1