3

This is my code for displaying Hijri date. The retrieved format for any Hijri month is in integer, but I need it to be in String, so I have added an array of Strings for Hijri month names.

    Chronology iso = ISOChronology.getInstanceUTC();
    Chronology hijri = IslamicChronology.getInstanceUTC();

    String [] hmonths= {"Muharram", "Safar", "Rabi al-Awwal", "Rabi al-Akhir", "Jamadi al-Awwal", "Jamadi al-Akhir", "Rajab", "Shabaan", "Ramadhan", "Shawwal", "Zilqad", "Zilhajj"};



    //month + 1  : because get month return month-1
    LocalDate todayIso = new LocalDate(c.get(c.YEAR),c.get(c.MONTH)+1), c.get(c.DAY_OF_MONTH), iso);
    LocalDate todayHijri = new LocalDate(todayIso.toDate(), hijri);
    DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMMM-yyyy");
    hijriView.setText(todayHijri.toString(fmt) );
Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43

1 Answers1

1

On StackOverflow I have found this question and related answers and hope it helps you. Unfortunately Joda-Time is obviously broken with regards to hijri month names.

Update on 2015-09-15:

More than one year later Joda-Time has still no real solution. Another reason why I have developed my own date-time-library Time4A (v3.8-2015f on Android). I have now myself solved it this way. The OP might not be active any longer here on SO but I hope it is useful for all people with a similar problem:

// determine current date (local timezone and start-of-day = 18:00 on previous day)
HijriCalendar todayHijri = 
  SystemClock.inLocalView().now(
    HijriCalendar.family(), 
    HijriCalendar.VARIANT_UMALQURA, 
    StartOfDay.EVENING
  ).toDate();

// standard localizable format (support for 46 languages)
ChronoFormatter<HijriCalendar> hijriFormat =
  ChronoFormatter.setUp(HijriCalendar.class, new Locale("ar"))
  .addPattern("d-MMMM-yyyy", PatternType.CLDR).build();
String hijriView = hijriFormat.format(todayHijri);
System.out.println("Hijri: " + hijriView); // Hijri: ٢-ذو الحجة-١٤٣٦
System.out.println("ISO: " + todayHijri.transform(PlainDate.class)); // ISO: 2015-09-15

// custom format with following month names:
Map<HijriMonth, String> monthNames = new HashMap<>();
monthNames.put(HijriMonth.MUHARRAM, "Muharram");
monthNames.put(HijriMonth.SAFAR, "Safar");
monthNames.put(HijriMonth.RABI_I, "Rabi al-Awwal");
monthNames.put(HijriMonth.RABI_II, "Rabi al-Akhir");
monthNames.put(HijriMonth.JUMADA_I, "Jamadi al-Awwal");
monthNames.put(HijriMonth.JUMADA_II, "Jamadi al-Akhir");
monthNames.put(HijriMonth.RAJAB, "Rajab");
monthNames.put(HijriMonth.SHABAN, "Shabaan");
monthNames.put(HijriMonth.RAMADAN, "Ramadhan");
monthNames.put(HijriMonth.SHAWWAL, "Shawwal");
monthNames.put(HijriMonth.DHU_AL_QIDAH, "Zilqad");
monthNames.put(HijriMonth.DHU_AL_HIJJAH, "Zilhajj");
ChronoFormatter<HijriCalendar> hijriFormat2 =
  ChronoFormatter.setUp(HijriCalendar.class, Locale.ROOT)
  .addInteger(HijriCalendar.DAY_OF_MONTH, 1, 2).addLiteral('-')
  .addText(HijriCalendar.MONTH_OF_YEAR, monthNames)
  .addPattern("-yyyy", PatternType.CLDR).build();
System.out.println(hijriFormat2.format(todayHijri)); // 2-Zilhajj-1436

Important: Hijri calendar knows different variants which lead to different days in gregorian calendar - given one Hijri date. Time4A actually offers 10 variants. The code example uses the Umalqura variant of Saudi-Arabia.

Community
  • 1
  • 1
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • but in that example there is no array of hijri months name – user3392787 Mar 09 '14 at 04:08
  • can u write the code for me... because i'm new in android AND JAVA – user3392787 Mar 09 '14 at 04:09
  • If you follow the given link there is at least a rough description of what to do. The work-around seems to be based on property files but can also changed to use a month name array as source. Anyway, my task is surely not correcting broken Joda due to my lack of time. The correction looks complex, unfortunately. Java 8 offers an Umalqura-calendar which works with the correct month names. Later I would also offer a hijri calendar built in my library Time4J, but at the moment other things have higher priority for me. – Meno Hochschild Mar 09 '14 at 18:02