public void test_weekOfMonth_buildDate_sun7() { // July 2011 starts on a Friday // Fri/Sat 1st/2nd are week 0 WeekRules wr = WeekRules.of(SUNDAY, 7); for (int i = 1; i <= 31; i++) { int w = MathUtils.floorDiv(i - 3, 7) + 1; int d = MathUtils.floorMod(i - 3, 7) + 1; CalendricalEngine engine = CalendricalEngine.merge( YearMonth.of(2011, JULY), wr.weekOfMonth().field(w), wr.dayOfWeek().field(d)); assertEquals(engine.derive(LocalDate.rule()), LocalDate.of(2011, JULY, i)); } }
public void test_weekOfMonth_getValue_sun7() { // July 2011 starts on a Friday // Fri/Sat 1st/2nd are week 0 DateTimeRule rule = WeekRules.of(SUNDAY, 7).weekOfMonth(); for (int i = 1; i <= 31; i++) { int w = MathUtils.floorDiv(i - 3, 7) + 1; // 3rd is start of week 1 assertEquals(rule.getValue(LocalDate.of(2011, 7, i)), rule.field(w)); } }
/** * Calculates the week of week-based-year. * * @param date the date to use, not null * @return the week */ static int getWeekOfWeekBasedYearFromDate(LocalDate date) { int wby = getWeekBasedYearFromDate(date); LocalDate yearStart = LocalDate.of(wby, MonthOfYear.JANUARY, 4); return MathUtils.safeToInt( (date.toModifiedJulianDay() - yearStart.toModifiedJulianDay() + yearStart.getDayOfWeek().getValue() - 1) / 7 + 1); }
/** * Gets the length of this week-based-year in weeks. * * @return the length of this week-based-year in weeks, either 52 or 53 */ public int lengthInWeeks() { // TODO: optimize LocalDate start = LocalDate.of(weekyear, MonthOfYear.JANUARY, 4); LocalDate end = LocalDate.of(weekyear, MonthOfYear.DECEMBER, 28); long weeksAsLong = (end.toModifiedJulianDay() + (8 - end.getDayOfWeek().getValue()) - start.toModifiedJulianDay() + start.getDayOfWeek().getValue() - 1) / 7; return MathUtils.safeToInt(weeksAsLong); }
/** * Sets the calendrical being output. * * @param calendrical the calendrical, not null */ public void setCalendrical(Calendrical calendrical) { MathUtils.checkNotNull(calendrical, "Calendrical must not be null"); this.calendrical = calendrical; }
/** * Sets the formatting symbols. * * <p>The symbols control the localization of numeric output. * * @param symbols the formatting symbols, not null */ public void setSymbols(DateTimeFormatSymbols symbols) { MathUtils.checkNotNull(symbols, "DateTimeFormatSymbols must not be null"); this.symbols = symbols; }
/** * Sets the locale. * * <p>This locale is used to control localization in the print output except where localization is * controlled by the symbols. * * @param locale the locale, not null */ public void setLocale(Locale locale) { MathUtils.checkNotNull(locale, "Locale must not be null"); this.locale = locale; }