Search code examples
javaandroidicu4j

get display name for month and day in week in icu4j for islamicCalendar


in icu4j we can get display month name by locale like this

 SimpleDateFormat formatter
                = new SimpleDateFormat ("yyyy MMM dd");
        String dateString = formatter.format(calendar.getTime());

which return current date with month name in geregorian calendar.
but Is it Possible to get month name of Islamic months using icu4j.


Solution

  • ULocale locale = new ULocale("@calendar=islamic");
    Calendar islamicCalendar = Calendar.getInstance(locale);
    
    // full date
    DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, locale);
    System.out.println(df.format(islamicCalendar.getTime()));
    
    // date in "yyyy MMM dd" format
    SimpleDateFormat df1 = new SimpleDateFormat ("yyyy MMM dd", locale);
    System.out.println(df1.format(islamicCalendar.getTime()));
    
    // name of month 
    SimpleDateFormat df2 = new SimpleDateFormat (SimpleDateFormat.MONTH, locale);
    System.out.println(df2.format(islamicCalendar.getTime()));
    
    // name of weekday
    SimpleDateFormat df3 = new SimpleDateFormat (SimpleDateFormat.WEEKDAY, locale);
    System.out.println(df3.format(islamicCalendar.getTime()));
    

    Outputs:

    AH 1438 Rabiʻ I 5, Sun
    
    1438 Rab. I 05
    
    Rabiʻ I
    
    Sun
    

    If you want output to be in a specific locale, put that locale before @calendar=islamic:

    Example for arabic locale:

    ULocale locale = new ULocale("ar@calendar=islamic");
    ...
    

    Outputs:

    الأحد، ٥ ربيع الأول، ١٤٣٨ هـ
    
    ١٤٣٨ ربيع الأول ٠٥
    
    ربيع الأول
    
    الأحد