MenoData / Time4J

Advanced date, time and interval library for Java with sun/moon-astronomy and calendars like Chinese, Coptic, Ethiopian, French Republican, Hebrew, Hijri, Historic Christian, Indian National, Japanese, Julian, Korean, Minguo, Persian, Thai, Vietnamese
GNU Lesser General Public License v2.1
424 stars 62 forks source link

How to convert english date to islamic. #887

Closed osmmosa closed 4 years ago

osmmosa commented 4 years ago

I want to convert 01 FEB 2020 to Islamic(Hijri) like 07-06-1441 and its event like السبت

MenoData commented 4 years ago

Following code should satisfy your requirements:

        // immutable so can be static final
        ChronoFormatter<PlainDate> parser =
            ChronoFormatter.ofDatePattern(
                "dd MMM uuuu", PatternType.CLDR, Locale.ENGLISH)
                // ignores uppercase versus lower case
                .with(Attributes.PARSE_CASE_INSENSITIVE, true);

        // immutable so can be static final
        ChronoFormatter<HijriCalendar> formatter =
            ChronoFormatter.ofPattern(
                "dd-MM-yyyy, EEEE", 
                PatternType.CLDR, 
                new Locale("ar", "EG"), // Egypt as country? If not then change the country.
                HijriCalendar.family())
                // enforces ordinary arabic digits 0-9 
                // otherwise the country in locale will determine it
                .with(Attributes.NUMBER_SYSTEM, NumberSystem.ARABIC);

        String input = "01 FEB 2020";
        PlainDate gregorian = parser.parse(input);
        // if you want the calendar of Saudi-Arabia then specify the variant Ummalqura
        HijriCalendar hijri = 
          gregorian.transform(HijriCalendar.family(), HijriCalendar.VARIANT_UMALQURA);
        String output = formatter.print(hijri);
        System.out.println(output); // 07-06-1441, السبت

Note: According to Google translator, the Arabic word "السبت" means "Saturday" in English, so I suggest the pattern symbols "EEEE" indicating the day-of-week (4 letters for full name).