java.time.temporal.TemporalAdjuster Java Examples

The following examples show how to use java.time.temporal.TemporalAdjuster. 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: TCKChronoZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #2
Source File: TCKChronoLocalDate.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #3
Source File: TCKChronoLocalDateTime.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #4
Source File: TCKChronoLocalDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #5
Source File: TCKChronoLocalDate.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #6
Source File: TCKChronoZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@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");
        }
    }
}
 
Example #7
Source File: TCKLocalTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: ChronoLocalDateTimeImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #9
Source File: TestKata5StreamsInDateTime.java    From java-katas with MIT License 5 votes vote down vote up
@Test
@Tag("TODO")
@Order(2)
public void verifyStreamOperationsOnTemporalAdjustment() {

    TemporalAdjuster nextOrSameSunday = temporal -> {
        LocalDate localDate = LocalDate.from(temporal);
        // TODO: Add a localDate manipulation that returns
        //  either the next sunday after current date
        //  it is a or current date, if Sunday.
        //  Check: java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
        //  Check: java.time.temporal.TemporalAdjusters.nextOrSame(DayOfWeek)
        // HINT: localDate = localDate.???(TemporalAdjusters.nextOrSame(???));
        return temporal.with(localDate);
    };

    Stream<LocalDate> someDates = Stream.of(
            LocalDate.of(1997, 8, 29),
            LocalDate.of(2015, 1, 1),
            LocalDate.of(2015, 1, 4));

    // TODO: Fix the list mapping below.
    //  Check: java.time.LocalDate.with(java.time.temporal.TemporalAdjuster)
    List<LocalDate> collectSundays = someDates
            .map(each -> each)
            .collect(Collectors.toList());

    assertEquals("1997-08-31",
            collectSundays.get(0).toString(),
            "Sunday on or after 1997-08-29 is 1997-08-31");

    assertEquals("2015-01-04",
            collectSundays.get(1).toString(),
            "Sunday on or after 2015-01-01 is 2015-01-04");

    assertEquals("2015-01-04",
            collectSundays.get(2).toString(),
            "Sunday on or after 1997-01-04 is 2015-01-04");
}
 
Example #10
Source File: ChronoLocalDateTimeImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example #11
Source File: TCKOffsetDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: DateTimes.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static LocalDate getDayAfterDays(LocalDate startDate, int days) {

        if (startDate == null) {
            throw new IllegalArgumentException("Start date cannot be null");
        }

        Period period = Period.ofDays(days);
        TemporalAdjuster ta = p -> p.plus(period);
        LocalDate endDate = startDate.with(ta);

        return endDate;
    }
 
Example #13
Source File: TCKLocalDate.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #14
Source File: TCKOffsetTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #15
Source File: TCKOffsetTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example #16
Source File: TCKOffsetTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #17
Source File: ChronoLocalDateTimeImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example #18
Source File: TCKOffsetDateTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: TCKLocalDateTime.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #20
Source File: TCKLocalTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #21
Source File: TCKLocalDate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #22
Source File: TCKLocalDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: TCKLocalDate.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
    TEST_2007_07_15.with((TemporalAdjuster) null);
}
 
Example #24
Source File: TCKLocalTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
    TEST_12_30_40_987654321.with((TemporalAdjuster) null);
}
 
Example #25
Source File: TCKTemporalAdjusters.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factory_ofDateAdjuster() {
    TemporalAdjuster test = TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
    assertEquals(LocalDate.of(2012, 6, 30).with(test), LocalDate.of(2012, 7, 2));
}
 
Example #26
Source File: ChronoLocalDateImpl.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public D with(TemporalAdjuster adjuster) {
    return (D) ChronoLocalDate.super.with(adjuster);
}
 
Example #27
Source File: TCKZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjuster_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.with((TemporalAdjuster) null);
}
 
Example #28
Source File: TCKZonedDateTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjuster_null() {
    ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100);
    base.with((TemporalAdjuster) null);
}
 
Example #29
Source File: ChronoLocalDateImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public D with(TemporalAdjuster adjuster) {
    return (D) ChronoLocalDate.super.with(adjuster);
}
 
Example #30
Source File: TCKLocalTime.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_with_adjustment_null() {
    TEST_12_30_40_987654321.with((TemporalAdjuster) null);
}