javaandroiddatecalendartime4j

Get week in month in hijri calendar in time4j


I'm trying to set hijri calendar to for example 3rd Wednesday of hijri months.
After some search, I reach to this code:

 PlainDate date = PlainDate.of(2017, 3, 1); // first of march 2017
 System.out.println(date.with(WEEKDAY_IN_MONTH.setToThird(WEDNESDAY)));    

But as you can see this sets calendar to 3rd Wednesday of gregorian calendar.
Is there any way to set 3rd Wednesday of month for other calendars in time4j lib?


Solution

  • Sorry for late answer although the solution is already available since end of July 2017 in release v4.28/3.33. (I am now busy with overnext release v4.29/3.34 (sunrise/sunset-calculations)).

    Every month-based calendar (including the HijriCalendar) supports a WEEKDAY_IN_MONTH-element since Time4A-version v3.33-2017b. It is chronology-specific because the month definition is different for every calendar. Example:

    HijriCalendar hijri =
      HijriCalendar.of(
        HijriCalendar.VARIANT_UMALQURA, 1395, HijriMonth.RAMADAN, 1); // Sunday, 1975-09-07
    assertThat(
      hijri.with(HijriCalendar.WEEKDAY_IN_MONTH.setTo(3, Weekday.WEDNESDAY)),
      is(hijri.plus(17, HijriCalendar.Unit.DAYS))); // AH-1395-09-18
    

    More examples how to use the new element can be found in the JUnit-test. The new element can also be used in format patterns with the CLDR-pattern-symbol "F". I show both the builder- and the pattern-based approach:

        ChronoFormatter<HijriCalendar> f1 =
            ChronoFormatter.setUp(HijriCalendar.family(), Locale.ENGLISH)
                .addEnglishOrdinal(HijriCalendar.WEEKDAY_IN_MONTH)
                .addPattern(" EEEE 'in' MMMM", PatternType.CLDR)
                .build();
        assertThat(f1.format(hijri), is("1st Sunday in Ramadan"));
    
        ChronoFormatter<HijriCalendar> f2 =
            ChronoFormatter.ofPattern(
                "F. EEEE 'im' MMMM", 
                PatternType.CLDR, 
                Locale.GERMAN, 
                HijriCalendar.family());
        assertThat(f2.format(hijri), is("1. Sonntag im Ramadan"));
    

    Side note: The new FrenchRepublicanCalendar (also introduced in v4.28/3.33) has no support for WEEKDAY_IN_MONTH because a) it uses a 10-day-week and b) some days (the sansculottides) are not part of any month.