List of usage examples for java.time.temporal Temporal with
default Temporal with(TemporalAdjuster adjuster)
From source file:Main.java
public static void main(String[] args) { TemporalQuery<Integer> daysBeforeChristmas = new TemporalQuery<Integer>() { public int daysTilChristmas(int acc, Temporal temporal) { int month = temporal.get(ChronoField.MONTH_OF_YEAR); int day = temporal.get(ChronoField.DAY_OF_MONTH); int max = temporal.with(TemporalAdjusters.lastDayOfMonth()).get(ChronoField.DAY_OF_MONTH); if (month == 12 && day <= 25) return acc + (25 - day); return daysTilChristmas(acc + (max - day + 1), temporal.with(TemporalAdjusters.firstDayOfNextMonth())); }/* www . java 2 s . c om*/ @Override public Integer queryFrom(TemporalAccessor temporal) { if (!(temporal instanceof Temporal)) throw new RuntimeException("Temporal accessor must be of type Temporal"); return daysTilChristmas(0, (Temporal) temporal); } }; System.out.println(LocalDate.of(2013, 12, 26).query(daysBeforeChristmas)); // 364 System.out.println(LocalDate.of(2013, 12, 23).query(daysBeforeChristmas)); // 2 System.out.println(LocalDate.of(2013, 12, 25).query(daysBeforeChristmas)); // 0 System.out.println(ZonedDateTime.of(2013, 12, 1, 11, 0, 13, 938282, ZoneId.of("America/Los_Angeles")) .query(daysBeforeChristmas)); // 24 }
From source file:Main.java
@Override public Temporal adjustInto(Temporal input) { LocalDate date = LocalDate.from(input); LocalDate nextMonth = date.plusMonths(1); LocalDate firstTuesdayInNextMonth = nextMonth.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY)); return input.with(firstTuesdayInNextMonth); }
From source file:Main.java
/** * The adjustInto method accepts a Temporal instance * and returns an adjusted LocalDate. If the passed in * parameter is not a LocalDate, then a DateTimeException is thrown. *//*from w ww . ja va 2 s . co m*/ public Temporal adjustInto(Temporal input) { LocalDate date = LocalDate.from(input); int day; if (date.getDayOfMonth() < 15) { day = 15; } else { day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth(); } date = date.withDayOfMonth(day); if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) { date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)); } return input.with(date); }
From source file:org.silverpeas.core.calendar.Recurrence.java
private Temporal computeEndDate() { Temporal date = this.getStartDate(); if (getRecurrenceCount() == 1) { return date; }/* w ww. java2 s . c o m*/ final long interval = (long) getRecurrenceCount() * (getFrequency().getInterval() >= 1 ? getFrequency().getInterval() : 1); date = date.plus(interval, getFrequency().getUnit().toChronoUnit()); boolean firstDayOfWeekSet = false; for (DayOfWeekOccurrence dayOfWeek : daysOfWeek) { Temporal current = date.with(dayOfWeek.dayOfWeek()); if (getFrequency().isMonthly()) { current = computeDateForMonthlyFrequencyFrom(current, dayOfWeek); } else if (getFrequency().isYearly()) { current = computeDateForYearlyFrequencyFrom(current, dayOfWeek); } if (!firstDayOfWeekSet || LocalDate.from(current).isAfter(LocalDate.from(date))) { date = current; firstDayOfWeekSet = true; } } return date; }