Java Code Examples for java.time.DayOfWeek#getValue()

The following examples show how to use java.time.DayOfWeek#getValue() . 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: ExchangeContract.java    From java-trader with Apache License 2.0 6 votes vote down vote up
public boolean isAfterLastTradingDay(LocalDate tradingDay) {
    boolean result = false;
    if ( getLastTradingDayOfMonth()>0 && tradingDay.getDayOfMonth()>=getLastTradingDayOfMonth() ) {
        result = true;
    }
    DayOfWeek dayOfWeek = tradingDay.getDayOfWeek();
    int weekOfMonth = tradingDay.get(WeekFields.of(DayOfWeek.SUNDAY, 2).weekOfMonth());
    if (getLastTradingWeekOfMonth() > 0 &&
            (weekOfMonth > getLastTradingWeekOfMonth()
                    || (weekOfMonth == getLastTradingWeekOfMonth() && dayOfWeek.getValue() > getLastTradingDayOfWeek().getValue()))
       )
    {
        result = true;
    }
    return result;
}
 
Example 2
Source File: TCKWeekFields.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
 * and WeekBasedYear for every combination of start of week
 * and minimal days in week.
 * @param firstDayOfWeek the first day of the week
 * @param minDays the minimum number of days in the week
 */
@Test(dataProvider="weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;

        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n",
                weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 3
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
 * and WeekBasedYear for every combination of start of week
 * and minimal days in week.
 * @param firstDayOfWeek the first day of the week
 * @param minDays the minimum number of days in the week
 */
@Test(dataProvider="weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;

        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n",
                weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 4
Source File: TCKWeekFields.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
 * and WeekBasedYear for every combination of start of week
 * and minimal days in week.
 * @param firstDayOfWeek the first day of the week
 * @param minDays the minimum number of days in the week
 */
@Test(dataProvider="weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;

        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n",
                weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 5
Source File: ExecutionTimeCron4jIntegrationTest.java    From cron-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Issue #203: cron4j definition should generate next execution times matching both the day of month and day of week when both are restricted.
 */
@Test
public void testRandomDayOfMonthAndDayOfWeek() {
    // pick a random day of week and day of month
    // DayOfWeek uses 1 (Mon) to 7 (Sun) while cron4j allows 0 (Sun) to 6 (Sat)
    final Random random = new Random();
    final DayOfWeek dayOfWeek = DayOfWeek.of(random.nextInt(7) + 1);
    int dayOfWeekValue = dayOfWeek.getValue();
    if (dayOfWeekValue == 7) {
        dayOfWeekValue = 0;
    }
    final int month = random.nextInt(12) + 1;
    // using max length so it is possible to use February 29 (leap-year)
    final int dayOfMonth = random.nextInt(Month.of(month).maxLength()) + 1;
    final String expression = String.format("0 0 %d %d %d", dayOfMonth, month, dayOfWeekValue);
    final ExecutionTime executionTime = ExecutionTime.forCron(cron4jCronParser.parse(expression));
    log.debug("cron4j expression: {}", expression);
    ZonedDateTime next = ZonedDateTime.now();
    log.debug("Start date: {}", next);
    for (int i = 1; i <= 20; i++) {
        final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(next);
        assert (nextExecution.isPresent());
        next = nextExecution.get();
        log.debug("Execution #{} date: {}", i, next);
        assertEquals("Incorrect day of the week", dayOfWeek, next.getDayOfWeek());
        assertEquals("Incorrect day of the month", dayOfMonth, next.getDayOfMonth());
        assertEquals("Incorrect month", month, next.getMonthValue());
    }
}
 
Example 6
Source File: TCKWeekFields.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
 * and WeekBasedYear for every combination of start of week
 * and minimal days in week.
 * @param firstDayOfWeek the first day of the week
 * @param minDays the minimum number of days in the week
 */
@Test(dataProvider="weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;

        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n",
                weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 7
Source File: DayOfWeekExtractor.java    From tutorials with MIT License 4 votes vote down vote up
public static int getDayNumberNew(LocalDate date) {
    DayOfWeek day = date.getDayOfWeek();
    return day.getValue();
}
 
Example 8
Source File: TemporalAdjusters.java    From jdk8u_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the previous day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-08 (seven days earlier).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the previous day-of-week adjuster, not null
 */
public static TemporalAdjuster previous(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 9
Source File: TemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the next day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week after the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-22 (seven days later).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the next day-of-week adjuster, not null
 */
public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = calDow - dowValue;
        return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 10
Source File: TemporalAdjusters.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the next day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week after the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-22 (seven days later).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the next day-of-week adjuster, not null
 */
public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = calDow - dowValue;
        return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 11
Source File: TemporalAdjusters.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the previous-or-same day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted
 * unless it is already on that day in which case the same object is returned.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to check for or move the date to, not null
 * @return the previous-or-same day-of-week adjuster, not null
 */
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        if (calDow == dowValue) {
            return temporal;
        }
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 12
Source File: TemporalAdjusters.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the previous-or-same day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted
 * unless it is already on that day in which case the same object is returned.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to check for or move the date to, not null
 * @return the previous-or-same day-of-week adjuster, not null
 */
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        if (calDow == dowValue) {
            return temporal;
        }
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 13
Source File: TemporalAdjusters.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the next day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week after the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-22 (seven days later).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the next day-of-week adjuster, not null
 */
public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = calDow - dowValue;
        return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 14
Source File: TemporalAdjusters.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Returns the previous day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-08 (seven days earlier).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the previous day-of-week adjuster, not null
 */
public static TemporalAdjuster previous(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 15
Source File: TemporalAdjusters.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the previous day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-08 (seven days earlier).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the previous day-of-week adjuster, not null
 */
public static TemporalAdjuster previous(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 16
Source File: TemporalAdjusters.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the previous day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-08 (seven days earlier).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the previous day-of-week adjuster, not null
 */
public static TemporalAdjuster previous(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 17
Source File: TemporalAdjusters.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the next day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week after the date being adjusted.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-17 (two days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-19 (four days later).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-22 (seven days later).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to move the date to, not null
 * @return the next day-of-week adjuster, not null
 */
public static TemporalAdjuster next(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        int daysDiff = calDow - dowValue;
        return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 18
Source File: TemporalAdjusters.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the previous-or-same day-of-week adjuster, which adjusts the date to the
 * first occurrence of the specified day-of-week before the date being adjusted
 * unless it is already on that day in which case the same object is returned.
 * <p>
 * The ISO calendar system behaves as follows:<br>
 * The input 2011-01-15 (a Saturday) for parameter (MONDAY) will return 2011-01-10 (five days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (WEDNESDAY) will return 2011-01-12 (three days earlier).<br>
 * The input 2011-01-15 (a Saturday) for parameter (SATURDAY) will return 2011-01-15 (same as input).
 * <p>
 * The behavior is suitable for use with most calendar systems.
 * It uses the {@code DAY_OF_WEEK} field and the {@code DAYS} unit,
 * and assumes a seven day week.
 *
 * @param dayOfWeek  the day-of-week to check for or move the date to, not null
 * @return the previous-or-same day-of-week adjuster, not null
 */
public static TemporalAdjuster previousOrSame(DayOfWeek dayOfWeek) {
    int dowValue = dayOfWeek.getValue();
    return (temporal) -> {
        int calDow = temporal.get(DAY_OF_WEEK);
        if (calDow == dowValue) {
            return temporal;
        }
        int daysDiff = dowValue - calDow;
        return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    };
}
 
Example 19
Source File: PDTHelper.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Convert from {@link DayOfWeek} to Calendar day of week.
 *
 * @param eDOW
 *        Day of week. May not be <code>null</code>.
 * @return Something between Calendar.SUNDAY and Calendar.SATURDAY
 * @since 8.6.3
 */
public static int getCalendarDayOfWeek (@Nonnull final DayOfWeek eDOW)
{
  ValueEnforcer.notNull (eDOW, "DayOfWeek");
  return eDOW.getValue () % 7 + 1;
}
 
Example 20
Source File: DateTimeExtensions.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the {@link java.time.DayOfWeek} that is {@code days} many days after this day of the week.
 *
 * @param self a DayOfWeek
 * @param days the number of days to move forward
 * @return the DayOfWeek
 * @since 2.5.0
 */
public static DayOfWeek plus(final DayOfWeek self, int days) {
    int daysPerWeek = DayOfWeek.values().length;
    int val = ((self.getValue() + days - 1) % daysPerWeek) + 1;
    return DayOfWeek.of(val > 0 ? val : daysPerWeek + val);
}