@Test(dataProvider="calendars") public void test_badWithAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAdjuster adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { ChronoZonedDateTime<?> result = czdt.with(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badWithAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDate date2 = chrono2.date(refDate); TemporalAdjuster adjuster = new FixedAdjuster(date2); if (chrono != chrono2) { try { date.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException"); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate result = date.with(adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badWithAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAdjuster adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { cdt.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.with(adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
public static void findSpecificTimes() { //STATIC DAYS MonthDay zachBirthday = MonthDay.of(Month.MARCH, 15); MonthDay julyFourth = MonthDay.of(Month.JULY, 4); LocalDate zachBDay2016 = zachBirthday.atYear(2016); //DYNAMIC DAYS - Find the date for the next Thanksgiving TemporalAdjuster fourthThursdayAdjuster = TemporalAdjusters.dayOfWeekInMonth(4, THURSDAY); LocalDate thanksgiving = LocalDate.now().with(Month.NOVEMBER).with(fourthThursdayAdjuster); if (LocalDate.now().isAfter(thanksgiving)) { thanksgiving = thanksgiving.plusYears(1).with(fourthThursdayAdjuster); } //DYNAMIC DAYS WITH LAMBDA BASED INTERFACES LocalDate thanksgivingEasy = LocalDate.now().with(nextThanksgiving()); }
public static TemporalAdjuster nextThanksgiving() { return (temporal -> { if (temporal.isSupported(YEAR) && temporal.isSupported(MONTH_OF_YEAR) && temporal .isSupported(DAY_OF_MONTH)) { TemporalAdjuster fourthThursdayAdjuster = TemporalAdjusters.dayOfWeekInMonth(4, THURSDAY); Temporal thanksgiving = temporal.with(NOVEMBER).with(fourthThursdayAdjuster); if (LocalDate.from(temporal).isAfter(LocalDate.from(thanksgiving)) || temporal.equals(thanksgiving)) { thanksgiving = thanksgiving.plus(Period.ofYears(1)).with(fourthThursdayAdjuster); } return thanksgiving; } throw new UnsupportedTemporalTypeException( "Temporal must support Year, Month, and Day"); }); }
@SuppressWarnings("unchecked") @Override public ChronoLocalDateTimeImpl<D> with(TemporalAdjuster adjuster) { if (adjuster instanceof ChronoLocalDate) { // The Chronology is checked in with(date,time) return with((ChronoLocalDate) adjuster, time); } else if (adjuster instanceof LocalTime) { return with(date, (LocalTime) adjuster); } else if (adjuster instanceof ChronoLocalDateTimeImpl) { return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster); } return ChronoLocalDateTimeImpl.ensureValid(date.getChronology(), (ChronoLocalDateTimeImpl<?>) adjuster.adjustInto(this)); }
@Test public void test_with_adjustment() { final OffsetDateTime sample = OffsetDateTime.of(LocalDate.of(2012, 3, 4), LocalTime.of(23, 5), OFFSET_PONE); TemporalAdjuster adjuster = new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return sample; } }; assertEquals(TEST_2008_6_30_11_30_59_000000500.with(adjuster), sample); }
@Test public void test_with_adjustment() { final LocalDateTime sample = LocalDateTime.of(2012, 3, 4, 23, 5); TemporalAdjuster adjuster = new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return sample; } }; assertEquals(TEST_2007_07_15_12_30_40_987654321.with(adjuster), sample); }
@Test public void test_with_adjustment() { final LocalDate sample = LocalDate.of(2012, 3, 4); TemporalAdjuster adjuster = new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return sample; } }; assertEquals(TEST_2007_07_15.with(adjuster), sample); }
@Test public void test_with_adjustment() { final LocalTime sample = LocalTime.of(23, 5); TemporalAdjuster adjuster = new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return sample; } }; assertEquals(TEST_12_30_40_987654321.with(adjuster), sample); }
@Test public void test_with_adjustment() { final OffsetTime sample = OffsetTime.of(23, 5, 0, 0, OFFSET_PONE); TemporalAdjuster adjuster = new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return sample; } }; assertEquals(TEST_11_30_59_500_PONE.with(adjuster), sample); }
@Test public void test_with_adjustment_AmPm() { OffsetTime test = TEST_11_30_59_500_PONE.with(new TemporalAdjuster() { @Override public Temporal adjustInto(Temporal dateTime) { return dateTime.with(HOUR_OF_DAY, 23); } }); assertEquals(test, OffsetTime.of(23, 30, 59, 500, OFFSET_PONE)); }