org.threeten.bp.DayOfWeek Java Examples

The following examples show how to use org.threeten.bp.DayOfWeek. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Reads the state from the stream.
 *
 * @param in  the input stream, not null
 * @return the created object, not null
 * @throws IOException if an error occurs
 */
static ZoneOffsetTransitionRule readExternal(DataInput in) throws IOException {
    int data = in.readInt();
    Month month = Month.of(data >>> 28);
    int dom = ((data & (63 << 22)) >>> 22) - 32;
    int dowByte = (data & (7 << 19)) >>> 19;
    DayOfWeek dow = dowByte == 0 ? null : DayOfWeek.of(dowByte);
    int timeByte = (data & (31 << 14)) >>> 14;
    TimeDefinition defn = TimeDefinition.values()[(data & (3 << 12)) >>> 12];
    int stdByte = (data & (255 << 4)) >>> 4;
    int beforeByte = (data & (3 << 2)) >>> 2;
    int afterByte = (data & 3);
    int timeOfDaysSecs = (timeByte == 31 ? in.readInt() : timeByte * 3600);
    ZoneOffset std = (stdByte == 255 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds((stdByte - 128) * 900));
    ZoneOffset before = (beforeByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + beforeByte * 1800));
    ZoneOffset after = (afterByte == 3 ? ZoneOffset.ofTotalSeconds(in.readInt()) : ZoneOffset.ofTotalSeconds(std.getTotalSeconds() + afterByte * 1800));
    // only bit of validation that we need to copy from public of() method
    if (dom < -28 || dom > 31 || dom == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    LocalTime time = LocalTime.ofSecondOfDay(Jdk8Methods.floorMod(timeOfDaysSecs, SECS_PER_DAY));
    int adjustDays = Jdk8Methods.floorDiv(timeOfDaysSecs, SECS_PER_DAY);
    return new ZoneOffsetTransitionRule(month, dom, dow, time, adjustDays, defn, std, before, after);
}
 
Example #2
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_previous() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);

            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date);

                assertSame(test.getDayOfWeek(), dow, date + " " + test);

                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test);
                } else {
                    assertSame(month, Month.JANUARY);
                    assertTrue(date.getDayOfMonth() < 8);
                    assertEquals(test.getYear(), 2006);
                    assertSame(test.getMonth(), Month.DECEMBER);
                    assertTrue(test.getDayOfMonth() > 24);
                }
            }
        }
    }
}
 
Example #3
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_getters_floatingWeek() throws Exception {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(test.getMonth(), Month.MARCH);
    assertEquals(test.getDayOfMonthIndicator(), 20);
    assertEquals(test.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(test.getLocalTime(), TIME_0100);
    assertEquals(test.isMidnightEndOfDay(), false);
    assertEquals(test.getTimeDefinition(), TimeDefinition.WALL);
    assertEquals(test.getStandardOffset(), OFFSET_0200);
    assertEquals(test.getOffsetBefore(), OFFSET_0200);
    assertEquals(test.getOffsetAfter(), OFFSET_0300);
    assertSerializable(test);
}
 
Example #4
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_getters_floatingWeekBackwards() throws Exception {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, -1, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(test.getMonth(), Month.MARCH);
    assertEquals(test.getDayOfMonthIndicator(), -1);
    assertEquals(test.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(test.getLocalTime(), TIME_0100);
    assertEquals(test.isMidnightEndOfDay(), false);
    assertEquals(test.getTimeDefinition(), TimeDefinition.WALL);
    assertEquals(test.getStandardOffset(), OFFSET_0200);
    assertEquals(test.getOffsetBefore(), OFFSET_0200);
    assertEquals(test.getOffsetAfter(), OFFSET_0300);
    assertSerializable(test);
}
 
Example #5
Source File: TestStandardZoneRules.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void test_London_getTransitionRules() {
    ZoneRules test = europeLondon();
    List<ZoneOffsetTransitionRule> rules = test.getTransitionRules();
    assertEquals(rules.size(), 2);

    ZoneOffsetTransitionRule in = rules.get(0);
    assertEquals(in.getMonth(), Month.MARCH);
    assertEquals(in.getDayOfMonthIndicator(), 25);  // optimized from -1
    assertEquals(in.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(in.getLocalTime(), LocalTime.of(1, 0));
    assertEquals(in.getTimeDefinition(), TimeDefinition.UTC);
    assertEquals(in.getStandardOffset(), OFFSET_ZERO);
    assertEquals(in.getOffsetBefore(), OFFSET_ZERO);
    assertEquals(in.getOffsetAfter(), OFFSET_PONE);

    ZoneOffsetTransitionRule out = rules.get(1);
    assertEquals(out.getMonth(), Month.OCTOBER);
    assertEquals(out.getDayOfMonthIndicator(), 25);  // optimized from -1
    assertEquals(out.getDayOfWeek(), DayOfWeek.SUNDAY);
    assertEquals(out.getLocalTime(), LocalTime.of(1, 0));
    assertEquals(out.getTimeDefinition(), TimeDefinition.UTC);
    assertEquals(out.getStandardOffset(), OFFSET_ZERO);
    assertEquals(out.getOffsetBefore(), OFFSET_PONE);
    assertEquals(out.getOffsetAfter(), OFFSET_ZERO);
}
 
Example #6
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
ZoneRulesBuilder addRuleToWindow(
        int startYear,
        int endYear,
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        int adjustDays,
        TimeDefinition timeDefinition,
        int savingAmountSecs) {
    Jdk8Methods.requireNonNull(month, "month");
    Jdk8Methods.requireNonNull(timeDefinition, "timeDefinition");
    YEAR.checkValidValue(startYear);
    YEAR.checkValidValue(endYear);
    if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    if (windowList.isEmpty()) {
        throw new IllegalStateException("Must add a window before adding a rule");
    }
    TZWindow window = windowList.get(windowList.size() - 1);
    window.addRule(startYear, endYear, month, dayOfMonthIndicator, dayOfWeek, time, adjustDays, timeDefinition, savingAmountSecs);
    return this;
}
 
Example #7
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Obtains an instance defining the yearly rule to create transitions between two offsets.
 * <p>
 * Applications should normally obtain an instance from {@link ZoneRules}.
 * This factory is only intended for use when creating {@link ZoneRules}.
 *
 * @param month  the month of the month-day of the first day of the cutover week, not null
 * @param dayOfMonthIndicator  the day of the month-day of the cutover week, positive if the week is that
 *  day or later, negative if the week is that day or earlier, counting from the last day of the month,
 *  from -28 to 31 excluding 0
 * @param dayOfWeek  the required day-of-week, null if the month-day should not be changed
 * @param time  the cutover time in the 'before' offset, not null
 * @param timeEndOfDay  whether the time is midnight at the end of day
 * @param timeDefnition  how to interpret the cutover
 * @param standardOffset  the standard offset in force at the cutover, not null
 * @param offsetBefore  the offset before the cutover, not null
 * @param offsetAfter  the offset after the cutover, not null
 * @return the rule, not null
 * @throws IllegalArgumentException if the day of month indicator is invalid
 * @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
 */
public static ZoneOffsetTransitionRule of(
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        boolean timeEndOfDay,
        TimeDefinition timeDefnition,
        ZoneOffset standardOffset,
        ZoneOffset offsetBefore,
        ZoneOffset offsetAfter) {
    Jdk8Methods.requireNonNull(month, "month");
    Jdk8Methods.requireNonNull(time, "time");
    Jdk8Methods.requireNonNull(timeDefnition, "timeDefnition");
    Jdk8Methods.requireNonNull(standardOffset, "standardOffset");
    Jdk8Methods.requireNonNull(offsetBefore, "offsetBefore");
    Jdk8Methods.requireNonNull(offsetAfter, "offsetAfter");
    if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
        throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
    }
    return new ZoneOffsetTransitionRule(month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay ? 1 : 0, timeDefnition, standardOffset, offsetBefore, offsetAfter);
}
 
Example #8
Source File: ZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates an instance defining the yearly rule to create transitions between two offsets.
 *
 * @param month  the month of the month-day of the first day of the cutover week, not null
 * @param dayOfMonthIndicator  the day of the month-day of the cutover week, positive if the week is that
 *  day or later, negative if the week is that day or earlier, counting from the last day of the month,
 *  from -28 to 31 excluding 0
 * @param dayOfWeek  the required day-of-week, null if the month-day should not be changed
 * @param time  the cutover time in the 'before' offset, not null
 * @param adjustDays  the time days adjustment
 * @param timeDefnition  how to interpret the cutover
 * @param standardOffset  the standard offset in force at the cutover, not null
 * @param offsetBefore  the offset before the cutover, not null
 * @param offsetAfter  the offset after the cutover, not null
 * @throws IllegalArgumentException if the day of month indicator is invalid
 * @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
 */
ZoneOffsetTransitionRule(
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        int adjustDays,
        TimeDefinition timeDefnition,
        ZoneOffset standardOffset,
        ZoneOffset offsetBefore,
        ZoneOffset offsetAfter) {
    this.month = month;
    this.dom = (byte) dayOfMonthIndicator;
    this.dow = dayOfWeek;
    this.time = time;
    this.adjustDays = adjustDays;
    this.timeDefinition = timeDefnition;
    this.standardOffset = standardOffset;
    this.offsetBefore = offsetBefore;
    this.offsetAfter = offsetAfter;
}
 
Example #9
Source File: CalendarPagerView.java    From material-calendarview with MIT License 6 votes vote down vote up
public CalendarPagerView(
    @NonNull MaterialCalendarView view,
    CalendarDay firstViewDay,
    DayOfWeek firstDayOfWeek,
    boolean showWeekDays) {
  super(view.getContext());

  this.mcv = view;
  this.firstViewDay = firstViewDay;
  this.firstDayOfWeek = firstDayOfWeek;
  this.showWeekDays = showWeekDays;

  setClipChildren(false);
  setClipToPadding(false);

  if (showWeekDays) {
    buildWeekDays(resetAndGetWorkingCalendar());
  }
  buildDayViews(dayViews, resetAndGetWorkingCalendar());
}
 
Example #10
Source File: TestTzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void test_parseDayOfWeek() throws Exception {
    TzdbZoneRulesCompiler test = new TzdbZoneRulesCompiler("2010c", new ArrayList<File>(), null, false);
    assertEquals(parseDayOfWeek(test, "Mon"), DayOfWeek.MONDAY);
    assertEquals(parseDayOfWeek(test, "Tue"), DayOfWeek.TUESDAY);
    assertEquals(parseDayOfWeek(test, "Wed"), DayOfWeek.WEDNESDAY);
    assertEquals(parseDayOfWeek(test, "Thu"), DayOfWeek.THURSDAY);
    assertEquals(parseDayOfWeek(test, "Fri"), DayOfWeek.FRIDAY);
    assertEquals(parseDayOfWeek(test, "Sat"), DayOfWeek.SATURDAY);
    assertEquals(parseDayOfWeek(test, "Sun"), DayOfWeek.SUNDAY);
    assertEquals(parseDayOfWeek(test, "Monday"), DayOfWeek.MONDAY);
    assertEquals(parseDayOfWeek(test, "Tuesday"), DayOfWeek.TUESDAY);
    assertEquals(parseDayOfWeek(test, "Wednesday"), DayOfWeek.WEDNESDAY);
    assertEquals(parseDayOfWeek(test, "Thursday"), DayOfWeek.THURSDAY);
    assertEquals(parseDayOfWeek(test, "Friday"), DayOfWeek.FRIDAY);
    assertEquals(parseDayOfWeek(test, "Saturday"), DayOfWeek.SATURDAY);
    assertEquals(parseDayOfWeek(test, "Sunday"), DayOfWeek.SUNDAY);
    assertEquals(parseDayOfWeek(test, "Mond"), DayOfWeek.MONDAY);
    assertEquals(parseDayOfWeek(test, "Monda"), DayOfWeek.MONDAY);
}
 
Example #11
Source File: HijrahDate.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructs an instance with the specified date.
 *
 * @param gregorianDay  the number of days from 0001/01/01 (Gregorian), caller calculated
 */
private HijrahDate(long gregorianDay) {
    int[] dateInfo = getHijrahDateInfo(gregorianDay);

    checkValidYearOfEra(dateInfo[1]);
    checkValidMonth(dateInfo[2]);
    checkValidDayOfMonth(dateInfo[3]);
    checkValidDayOfYear(dateInfo[4]);

    this.era = HijrahEra.of(dateInfo[0]);
    this.yearOfEra = dateInfo[1];
    this.monthOfYear = dateInfo[2];
    this.dayOfMonth = dateInfo[3];
    this.dayOfYear = dateInfo[4];
    this.dayOfWeek = DayOfWeek.of(dateInfo[5]);
    this.gregorianEpochDay = gregorianDay;
    this.isLeapYear = isLeapYear(this.yearOfEra);
}
 
Example #12
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_positive")
public void test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.firstInMonth(dow).adjustInto(date);
        assertEquals(test, expected, "day-of-month=" + day);
    }
}
 
Example #13
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_createTransition_floatingWeekBackwards_secondLast() {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, -2, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    ZoneOffsetTransition trans = new ZoneOffsetTransition(
            LocalDateTime.of(2000, Month.MARCH, 26, 1, 0), OFFSET_0200, OFFSET_0300);
    assertEquals(test.createTransition(2000), trans);
}
 
Example #14
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_serialization_format() throws ClassNotFoundException, IOException {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
                    Month.MARCH, 20, DayOfWeek.TUESDAY, LocalTime.of(13, 34, 56), false, TimeDefinition.STANDARD,
                    OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEqualsSerialisedForm(test);
}
 
Example #15
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_equals_dayOfMonthDifferent() {
    ZoneOffsetTransitionRule a = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    ZoneOffsetTransitionRule b = ZoneOffsetTransitionRule.of(
            Month.MARCH, 21, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(a.equals(a), true);
    assertEquals(a.equals(b), false);
    assertEquals(b.equals(a), false);
    assertEquals(b.equals(b), true);
}
 
Example #16
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_equals_monthDifferent() {
    ZoneOffsetTransitionRule a = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    ZoneOffsetTransitionRule b = ZoneOffsetTransitionRule.of(
            Month.APRIL, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(a.equals(a), true);
    assertEquals(a.equals(b), false);
    assertEquals(b.equals(a), false);
    assertEquals(b.equals(b), true);
}
 
Example #17
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_toString_floatingWeekBackwards_last() {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, -1, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(test.toString(), "TransitionRule[Gap +02:00 to +03:00, SUNDAY on or before last day of MARCH at 01:00 WALL, standard offset +02:00]");
}
 
Example #18
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_createTransition_floatingWeek_overlap_endOfDay() {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, LocalTime.MIDNIGHT, true, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0300, OFFSET_0200);
    ZoneOffsetTransition trans = new ZoneOffsetTransition(
            LocalDateTime.of(2000, Month.MARCH, 27, 0, 0), OFFSET_0300, OFFSET_0200);
    assertEquals(test.createTransition(2000), trans);
}
 
Example #19
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_createTransition_floatingWeek_gap_notEndOfDay() {
    ZoneOffsetTransitionRule test = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    ZoneOffsetTransition trans = new ZoneOffsetTransition(
            LocalDateTime.of(2000, Month.MARCH, 26, 1, 0), OFFSET_0200, OFFSET_0300);
    assertEquals(test.createTransition(2000), trans);
}
 
Example #20
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_equals_null_false() {
    ZoneOffsetTransitionRule a = ZoneOffsetTransitionRule.of(
            Month.MARCH, 20, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(a.equals(null), false);
}
 
Example #21
Source File: TestIsoFields.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider="week")
public void test_parse_weeks(LocalDate date, DayOfWeek dow, int week, int wby) {
    DateTimeFormatter f = new DateTimeFormatterBuilder()
            .appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-')
            .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-')
            .appendValue(DAY_OF_WEEK).toFormatter();
    LocalDate parsed = LocalDate.parse(wby + "-" + week + "-" + dow.getValue(), f);
    assertEquals(parsed, date);
}
 
Example #22
Source File: TestZoneOffsetTransitionRule.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test_hashCode_floatingWeekBackwards() {
    ZoneOffsetTransitionRule a = ZoneOffsetTransitionRule.of(
            Month.MARCH, -1, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    ZoneOffsetTransitionRule b = ZoneOffsetTransitionRule.of(
            Month.MARCH, -1, DayOfWeek.SUNDAY, TIME_0100, false, TimeDefinition.WALL,
            OFFSET_0200, OFFSET_0200, OFFSET_0300);
    assertEquals(a.hashCode(), b.hashCode());
}
 
Example #23
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_negative")
public void test_lastInMonth(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.lastInMonth(dow).adjustInto(date);
        assertEquals(test, expected, "day-of-month=" + day);
    }
}
 
Example #24
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_negative")
public void test_dayOfWeekInMonth_negative(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int ordinal = 0; ordinal < 5; ordinal++) {
        for (int day = 1; day <= Month.of(month).length(false); day++) {
            LocalDate date = date(year, month, day);
            LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(-1 - ordinal, dow).adjustInto(date);
            assertEquals(test, expected.minusWeeks(ordinal));
        }
    }
}
 
Example #25
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_zero")
public void test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int day = 1; day <= Month.of(month).length(false); day++) {
        LocalDate date = date(year, month, day);
        LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(0, dow).adjustInto(date);
        assertEquals(test, expected);
    }
}
 
Example #26
Source File: TestTemporalAdjusters.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "dayOfWeekInMonth_positive")
public void test_dayOfWeekInMonth_positive(int year, int month, DayOfWeek dow, LocalDate expected) {
    for (int ordinal = 1; ordinal <= 5; ordinal++) {
        for (int day = 1; day <= Month.of(month).length(false); day++) {
            LocalDate date = date(year, month, day);
            LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(ordinal, dow).adjustInto(date);
            assertEquals(test, expected.plusWeeks(ordinal - 1));
        }
    }
}
 
Example #27
Source File: TzdbZoneRulesCompiler.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private DayOfWeek parseDayOfWeek(String str) {
    str = str.toLowerCase();
    for (DayOfWeek dow : DayOfWeek.values()) {
        if (matches(str, dow.name().toLowerCase())) {
            return dow;
        }
    }
    throw new IllegalArgumentException("Unknown day-of-week: " + str);
}
 
Example #28
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param year  the year
 * @param month  the month, not null
 * @param dayOfMonthIndicator  the day-of-month of the transition, adjusted by dayOfWeek,
 *   from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
 * @param dayOfWeek  the day-of-week, null if day-of-month is exact
 * @param time  the time, not null
 * @param adjustDays  the time day adjustment
 * @param timeDefinition  the time definition, not null
 * @param savingAfterSecs  the savings amount in seconds
 */
TZRule(int year, Month month, int dayOfMonthIndicator,
        DayOfWeek dayOfWeek, LocalTime time, int adjustDays,
        TimeDefinition timeDefinition, int savingAfterSecs) {
    super();
    this.year = year;
    this.month = month;
    this.dayOfMonthIndicator = dayOfMonthIndicator;
    this.dayOfWeek = dayOfWeek;
    this.time= time;
    this.adjustDays= adjustDays;
    this.timeDefinition = timeDefinition;
    this.savingAmountSecs = savingAfterSecs;
}
 
Example #29
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Adds a rule to the current window.
 *
 * @param startYear  the start year of the rule, from MIN_VALUE to MAX_VALUE
 * @param endYear  the end year of the rule, from MIN_VALUE to MAX_VALUE
 * @param month  the month of the transition, not null
 * @param dayOfMonthIndicator  the day-of-month of the transition, adjusted by dayOfWeek,
 *   from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
 * @param dayOfWeek  the day-of-week to adjust to, null if day-of-month should not be adjusted
 * @param time  the time that the transition occurs as defined by timeDefintion, not null
 * @param adjustDays  the time days adjustment
 * @param timeDefinition  the definition of how to convert local to actual time, not null
 * @param savingAmountSecs  the amount of saving from the standard offset in seconds
 * @throws IllegalStateException if the window already has fixed savings
 * @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules
 */
void addRule(
        int startYear,
        int endYear,
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        int adjustDays,
        TimeDefinition timeDefinition,
        int savingAmountSecs) {

    if (fixedSavingAmountSecs != null) {
        throw new IllegalStateException("Window has a fixed DST saving, so cannot have DST rules");
    }
    if (ruleList.size() >= 2000) {
        throw new IllegalStateException("Window has reached the maximum number of allowed rules");
    }
    boolean lastRule = false;
    if (endYear == Year.MAX_VALUE) {
        lastRule = true;
        endYear = startYear;
    }
    int year = startYear;
    while (year <= endYear) {
        TZRule rule = new TZRule(year, month, dayOfMonthIndicator, dayOfWeek, time, adjustDays, timeDefinition, savingAmountSecs);
        if (lastRule) {
            lastRuleList.add(rule);
            maxLastRuleStartYear = Math.max(startYear, maxLastRuleStartYear);
        } else {
            ruleList.add(rule);
        }
        year++;
    }
}
 
Example #30
Source File: ZoneRulesBuilder.java    From threetenbp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Adds a multi-year transition rule to the current window.
 * <p>
 * This adds a rule such that the offset, expressed as a daylight savings amount,
 * changes at the specified date-time for each year in the range.
 *
 * @param startYear  the start year of the rule, from MIN_VALUE to MAX_VALUE
 * @param endYear  the end year of the rule, from MIN_VALUE to MAX_VALUE
 * @param month  the month of the transition, not null
 * @param dayOfMonthIndicator  the day-of-month of the transition, adjusted by dayOfWeek,
 *   from 1 to 31 adjusted later, or -1 to -28 adjusted earlier from the last day of the month
 * @param dayOfWeek  the day-of-week to adjust to, null if day-of-month should not be adjusted
 * @param time  the time that the transition occurs as defined by timeDefintion, not null
 * @param timeEndOfDay  whether midnight is at the end of day
 * @param timeDefinition  the definition of how to convert local to actual time, not null
 * @param savingAmountSecs  the amount of saving from the standard offset after the transition in seconds
 * @return this, for chaining
 * @throws DateTimeException if a date-time field is out of range
 * @throws IllegalArgumentException if the day of month indicator is invalid
 * @throws IllegalArgumentException if the end of day midnight flag does not match the time
 * @throws IllegalStateException if no window has yet been added
 * @throws IllegalStateException if the window already has fixed savings
 * @throws IllegalStateException if the window has reached the maximum capacity of 2000 rules
 */
public ZoneRulesBuilder addRuleToWindow(
        int startYear,
        int endYear,
        Month month,
        int dayOfMonthIndicator,
        DayOfWeek dayOfWeek,
        LocalTime time,
        boolean timeEndOfDay,
        TimeDefinition timeDefinition,
        int savingAmountSecs) {
    Jdk8Methods.requireNonNull(month, "month");
    Jdk8Methods.requireNonNull(time, "time");
    Jdk8Methods.requireNonNull(timeDefinition, "timeDefinition");
    YEAR.checkValidValue(startYear);
    YEAR.checkValidValue(endYear);
    if (dayOfMonthIndicator < -28 || dayOfMonthIndicator > 31 || dayOfMonthIndicator == 0) {
        throw new IllegalArgumentException("Day of month indicator must be between -28 and 31 inclusive excluding zero");
    }
    if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
        throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
    }
    if (windowList.isEmpty()) {
        throw new IllegalStateException("Must add a window before adding a rule");
    }
    TZWindow window = windowList.get(windowList.size() - 1);
    window.addRule(startYear, endYear, month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay ? 1 : 0, timeDefinition, savingAmountSecs);
    return this;
}