如何使用Joda-Time将名称Hijri月显示为String?

时间:2014-03-08 11:59:56

标签: android string jodatime hijri

这是我显示Hijri日期的代码。任何Hijri月的检索格式都是整数,但我需要它在String中,所以我为Hijri月份名称添加了一个字符串数组。

    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) );

1 个答案:

答案 0 :(得分:1)

在StackOverflow上我找到了这个question and related answers并希望它可以帮到你。不幸的是,关于hijri月名,Joda-Time显然已被打破。

2015-09-15更新:

一年多以后,Joda-Time仍然没有真正的解决方案。我开发自己的日期时间库Time4A(Android上的v3.8-2015f)的另一个原因。我现在自己就这样解决了。 OP可能在此处不再活跃,但我希望它对所有遇到类似问题的人都有用:

// 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

重要事项: Hijri日历知道不同的变体,导致格里高利历中的不同日期 - 给定一个Hijri日期。 Time4A实际上提供了10种变体。代码示例使用沙特阿拉伯的Umalqura变体。