Android 以英文书写格式获取当前 Arabic/Islamic 日期

Android get current Arabic/Islamic date in English written format

我在查找当前伊斯兰日期时遇到问题,我从 Whosebug 进行了搜索并找到了一些解决方案,但这些并不是我需要的确切解决方案。这是我的代码,输出为 الأربعاء, أبريل ١٩, ٢٠١٧ 但我想将日期显示为“17th Jumada Al-Awwal 1438”格式。日期应该用英文写。

    Locale locale = new Locale("ar");
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM dd, yyy", locale);
    Date currDate = new Date();
    String formattedDate = sdf.format(currDate);

据我所知,Android 上没有对伊斯兰日历的内置支持,除非您愿意将您的应用限制为 API-level 24 (and if you like the old-fashioned API-style of ICU4J-class contributed by IBM ;-)). But maybe you can consider my library Time4A and then use this code (see also javadoc):

ChronoFormatter<HijriCalendar> hijriFormat =
    ChronoFormatter.setUp(HijriCalendar.family(), Locale.ENGLISH)
    .addEnglishOrdinal(HijriCalendar.DAY_OF_MONTH)
    .addPattern(" MMMM yyyy", PatternType.CLDR)
    .build()
    .withCalendarVariant(HijriCalendar.VARIANT_UMALQURA);

// conversion from gregorian to hijri-umalqura valid at noon 
// (not really valid in the evening when next islamic day starts)
HijriCalendar today = 
    SystemClock.inLocalView().today().transform(
        HijriCalendar.class,
        HijriCalendar.VARIANT_UMALQURA
    );
System.out.println(hijriFormat.format(today)); // 22nd Rajab 1438

// taking into account the specific start of day for Hijri calendar
HijriCalendar todayExact = 
    SystemClock.inLocalView().now(
        HijriCalendar.family(),
        HijriCalendar.VARIANT_UMALQURA,
        StartOfDay.EVENING // simple approximation => 18:00
    ).toDate();
System.out.println(hijriFormat.format(todayExact)); // 22nd Rajab 1438 (23rd after 18:00)

关于语言支持,所有文本资源都从 CLDR-v30-data (Unicode-consortium) 取代。例如在英语中:"Jumada II" 表示一年中的第 6 个月。如果你不喜欢它,你也可以提供你自己的文本。格式化程序生成器有一个 dedicated method 来执行此操作。