Example usage for java.time LocalDateTime with

List of usage examples for java.time LocalDateTime with

Introduction

In this page you can find the example usage for java.time LocalDateTime with.

Prototype

@Override
public LocalDateTime with(TemporalAdjuster adjuster) 

Source Link

Document

Returns an adjusted copy of this date-time.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 01);

    LocalDateTime t = a.with(TemporalAdjusters.firstDayOfMonth());

    System.out.println(t);//  w ww  .ja  v  a2  s .  co m
}

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}.
 * /*  w  w w  .  j  a  v a  2 s .  c om*/
 * @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
}

From source file:org.thevortex.lighting.jinks.robot.Recurrence.java

/**
 * Get the next occurrence from a time.//from   w  w w  . j  av a  2 s . c  om
 *
 * @param fromWhen when
 * @return the next occurrence or {@code null} if there is no more
 */
public LocalDateTime nextOccurrence(TemporalAccessor fromWhen) {
    LocalDateTime from = LocalDateTime.from(fromWhen);

    // if it's not today, try the next day
    if (frequency == Frequency.WEEKLY && !days.contains(from.getDayOfWeek())) {
        return nextOccurrence(from.plusDays(1).truncatedTo(ChronoUnit.DAYS));
    }

    // if we've already started, it's too late - next day
    if (from.toLocalTime().isAfter(startTime)) {
        return nextOccurrence(from.plusDays(1).truncatedTo(ChronoUnit.DAYS));
    }

    // otherwise, we're on the right day, so just adjust the time
    return from.with(startTime).truncatedTo(ChronoUnit.MINUTES);
}