Example usage for java.time.temporal TemporalAdjusters next

List of usage examples for java.time.temporal TemporalAdjusters next

Introduction

In this page you can find the example usage for java.time.temporal TemporalAdjusters next.

Prototype

public static TemporalAdjuster next(DayOfWeek dayOfWeek) 

Source Link

Document

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.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.of(2014, Month.FEBRUARY, 25); // 2014-02-25

    // next Sunday (2014-03-02)
    LocalDate nextSunday = date.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));

    System.out.println(nextSunday);
}

From source file:Main.java

public static void main(String[] args) {
    Month month = Month.valueOf("March".toUpperCase());

    System.out.printf("For the month of %s:%n", month);
    LocalDate date = Year.now().atMonth(month).atDay(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    Month mi = date.getMonth();/*from  w ww . j  av a 2  s  .c  om*/
    while (mi == month) {
        System.out.printf("%s%n", date);
        date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        mi = date.getMonth();
    }
}

From source file:ListMondays.java

public static void main(String[] args) {
    Month month = null;/*from  w  ww .j ava2  s  .com*/

    if (args.length < 1) {
        System.out.printf("Usage: ListMondays <month>%n");
        throw new IllegalArgumentException();
    }

    try {
        month = Month.valueOf(args[0].toUpperCase());
    } catch (IllegalArgumentException exc) {
        System.out.printf("%s is not a valid month.%n", args[0]);
        throw exc; // Rethrow the exception.
    }

    System.out.printf("For the month of %s:%n", month);
    LocalDate date = Year.now().atMonth(month).atDay(1).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
    Month mi = date.getMonth();
    while (mi == month) {
        System.out.printf("%s%n", date);
        date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        mi = date.getMonth();
    }
}

From source file:Main.java

/**
 * Gets the next or same closest date from the specified days in
 * {@code daysOfWeek List} at specified {@code hour} and {@code min}.
 * //from  w ww  .  j  av  a2  s  .  c  o  m
 * @param daysOfWeek
 *          the days of week
 * @param hour
 *          the hour
 * @param min
 *          the min
 * @return the next or same date from the days of week at specified time
 * @throws IllegalArgumentException
 *           if the {@code daysOfWeek List} is empty.
 */
public static LocalDateTime getNextClosestDateTime(List<DayOfWeek> daysOfWeek, int hour, int min)
        throws IllegalArgumentException {
    if (daysOfWeek.isEmpty()) {
        throw new IllegalArgumentException("daysOfWeek should not be empty.");
    }

    final LocalDateTime dateNow = LocalDateTime.now();
    final LocalDateTime dateNowWithDifferentTime = dateNow.withHour(hour).withMinute(min).withSecond(0);

    // @formatter:off
    return daysOfWeek.stream().map(d -> dateNowWithDifferentTime.with(TemporalAdjusters.nextOrSame(d)))
            .filter(d -> d.isAfter(dateNow)).min(Comparator.naturalOrder())
            .orElse(dateNowWithDifferentTime.with(TemporalAdjusters.next(daysOfWeek.get(0))));
    // @formatter:on
}