java.time.Year Java Examples

The following examples show how to use java.time.Year. 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: TCKYear.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider(name="minus_long_TemporalUnit")
Object[][] data_minus_long_TemporalUnit() {
    return new Object[][] {
        {Year.of(1), 1, ChronoUnit.YEARS, Year.of(0), null},
        {Year.of(1), -12, ChronoUnit.YEARS, Year.of(13), null},
        {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
        {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
        {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
        {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(999999999), null},
        {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(-999999999), null},

        {Year.of(999999999), 1, ChronoUnit.ERAS, Year.of(-999999999 + 1), null},
        {Year.of(105), 1, ChronoUnit.CENTURIES, Year.of(5), null},
        {Year.of(15), 1, ChronoUnit.DECADES, Year.of(5), null},

        {Year.of(-999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
        {Year.of(1), -999999999, ChronoUnit.YEARS, null, DateTimeException.class},

        {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
        {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
        {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
    };
}
 
Example #2
Source File: TCKYear.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@DataProvider(name="plus_long_TemporalUnit")
Object[][] data_plus_long_TemporalUnit() {
    return new Object[][] {
        {Year.of(1), 1, ChronoUnit.YEARS, Year.of(2), null},
        {Year.of(1), -12, ChronoUnit.YEARS, Year.of(-11), null},
        {Year.of(1), 0, ChronoUnit.YEARS, Year.of(1), null},
        {Year.of(999999999), 0, ChronoUnit.YEARS, Year.of(999999999), null},
        {Year.of(-999999999), 0, ChronoUnit.YEARS, Year.of(-999999999), null},
        {Year.of(0), -999999999, ChronoUnit.YEARS, Year.of(-999999999), null},
        {Year.of(0), 999999999, ChronoUnit.YEARS, Year.of(999999999), null},

        {Year.of(-1), 1, ChronoUnit.ERAS, Year.of(2), null},
        {Year.of(5), 1, ChronoUnit.CENTURIES, Year.of(105), null},
        {Year.of(5), 1, ChronoUnit.DECADES, Year.of(15), null},

        {Year.of(999999999), 1, ChronoUnit.YEARS, null, DateTimeException.class},
        {Year.of(-999999999), -1, ChronoUnit.YEARS, null, DateTimeException.class},

        {Year.of(1), 0, ChronoUnit.DAYS, null, DateTimeException.class},
        {Year.of(1), 0, ChronoUnit.WEEKS, null, DateTimeException.class},
        {Year.of(1), 0, ChronoUnit.MONTHS, null, DateTimeException.class},
    };
}
 
Example #3
Source File: TCKYear.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toString() {
    for (int i = -4; i <= 2104; i++) {
        Year a = Year.of(i);
        assertEquals(a.toString(), "" + i);
    }
}
 
Example #4
Source File: TCKYear.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_minusYears() {
    assertEquals(Year.of(2007).minusYears(-1), Year.of(2008));
    assertEquals(Year.of(2007).minusYears(0), Year.of(2007));
    assertEquals(Year.of(2007).minusYears(1), Year.of(2006));
    assertEquals(Year.of(2007).minusYears(2), Year.of(2005));

    assertEquals(Year.of(Year.MAX_VALUE - 1).minusYears(-1), Year.of(Year.MAX_VALUE));
    assertEquals(Year.of(Year.MAX_VALUE).minusYears(0), Year.of(Year.MAX_VALUE));

    assertEquals(Year.of(Year.MIN_VALUE + 1).minusYears(1), Year.of(Year.MIN_VALUE));
    assertEquals(Year.of(Year.MIN_VALUE).minusYears(0), Year.of(Year.MIN_VALUE));
}
 
Example #5
Source File: TCKYear.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_plusYears() {
    assertEquals(Year.of(2007).plusYears(-1), Year.of(2006));
    assertEquals(Year.of(2007).plusYears(0), Year.of(2007));
    assertEquals(Year.of(2007).plusYears(1), Year.of(2008));
    assertEquals(Year.of(2007).plusYears(2), Year.of(2009));

    assertEquals(Year.of(Year.MAX_VALUE - 1).plusYears(1), Year.of(Year.MAX_VALUE));
    assertEquals(Year.of(Year.MAX_VALUE).plusYears(0), Year.of(Year.MAX_VALUE));

    assertEquals(Year.of(Year.MIN_VALUE + 1).plusYears(-1), Year.of(Year.MIN_VALUE));
    assertEquals(Year.of(Year.MIN_VALUE).plusYears(0), Year.of(Year.MIN_VALUE));
}
 
Example #6
Source File: TCKMinguoChronology.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era  era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());

    MinguoDate minguo = MinguoDate.now();
    minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
    if (isLeapYear) {
        assertEquals(minguo.lengthOfMonth(), 29);
    } else {
        assertEquals(minguo.lengthOfMonth(), 28);
    }
}
 
Example #7
Source File: TCKYear.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toString() {
    for (int i = -4; i <= 2104; i++) {
        Year a = Year.of(i);
        assertEquals(a.toString(), "" + i);
    }
}
 
Example #8
Source File: TCKYear.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_atDay_notLeapYear() {
    Year test = Year.of(2007);
    LocalDate expected = LocalDate.of(2007, 1, 1);
    for (int i = 1; i <= 365; i++) {
        assertEquals(test.atDay(i), expected);
        expected = expected.plusDays(1);
    }
}
 
Example #9
Source File: TCKYear.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="isValidMonthDay")
Object[][] data_isValidMonthDay() {
    return new Object[][] {
            {Year.of(2007), MonthDay.of(6, 30), true},
            {Year.of(2008), MonthDay.of(2, 28), true},
            {Year.of(2008), MonthDay.of(2, 29), true},
            {Year.of(2009), MonthDay.of(2, 28), true},
            {Year.of(2009), MonthDay.of(2, 29), false},
            {Year.of(2009), null, false},
    };
}
 
Example #10
Source File: TCKYear.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_length() {
    assertEquals(Year.of(1999).length(), 365);
    assertEquals(Year.of(2000).length(), 366);
    assertEquals(Year.of(2001).length(), 365);

    assertEquals(Year.of(2007).length(), 365);
    assertEquals(Year.of(2008).length(), 366);
    assertEquals(Year.of(2009).length(), 365);
    assertEquals(Year.of(2010).length(), 365);
    assertEquals(Year.of(2011).length(), 365);
    assertEquals(Year.of(2012).length(), 366);

    assertEquals(Year.of(2095).length(), 365);
    assertEquals(Year.of(2096).length(), 366);
    assertEquals(Year.of(2097).length(), 365);
    assertEquals(Year.of(2098).length(), 365);
    assertEquals(Year.of(2099).length(), 365);
    assertEquals(Year.of(2100).length(), 365);
    assertEquals(Year.of(2101).length(), 365);
    assertEquals(Year.of(2102).length(), 365);
    assertEquals(Year.of(2103).length(), 365);
    assertEquals(Year.of(2104).length(), 366);
    assertEquals(Year.of(2105).length(), 365);

    assertEquals(Year.of(-500).length(), 365);
    assertEquals(Year.of(-400).length(), 366);
    assertEquals(Year.of(-300).length(), 365);
    assertEquals(Year.of(-200).length(), 365);
    assertEquals(Year.of(-100).length(), 365);
    assertEquals(Year.of(0).length(), 366);
    assertEquals(Year.of(100).length(), 365);
    assertEquals(Year.of(200).length(), 365);
    assertEquals(Year.of(300).length(), 365);
    assertEquals(Year.of(400).length(), 366);
    assertEquals(Year.of(500).length(), 365);
}
 
Example #11
Source File: TCKYear.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="isValidMonthDay")
Object[][] data_isValidMonthDay() {
    return new Object[][] {
            {Year.of(2007), MonthDay.of(6, 30), true},
            {Year.of(2008), MonthDay.of(2, 28), true},
            {Year.of(2008), MonthDay.of(2, 29), true},
            {Year.of(2009), MonthDay.of(2, 28), true},
            {Year.of(2009), MonthDay.of(2, 29), false},
            {Year.of(2009), null, false},
    };
}
 
Example #12
Source File: TCKOffsetDateTime.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_compareTo_max() {
    OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE);
    OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO);  // a is before b due to offset
    assertEquals(a.compareTo(b) < 0, true);
    assertEquals(b.compareTo(a) > 0, true);
    assertEquals(a.compareTo(a) == 0, true);
    assertEquals(b.compareTo(b) == 0, true);
}
 
Example #13
Source File: TCKMinguoChronology.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era  era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());

    MinguoDate minguo = MinguoDate.now();
    minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
    if (isLeapYear) {
        assertEquals(minguo.lengthOfMonth(), 29);
    } else {
        assertEquals(minguo.lengthOfMonth(), 28);
    }
}
 
Example #14
Source File: TCKZonedDateTime.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_with_adjuster_Year() {
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0);
    ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100);
    ZonedDateTime test = base.with(Year.of(2007));
    assertEquals(test, ZonedDateTime.of(ldt.withYear(2007), ZONE_0100));
}
 
Example #15
Source File: TCKOffsetDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_compareTo_min() {
    OffsetDateTime a = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PTWO);
    OffsetDateTime b = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PONE);  // a is before b due to offset
    assertEquals(a.compareTo(b) < 0, true);
    assertEquals(b.compareTo(a) > 0, true);
    assertEquals(a.compareTo(a) == 0, true);
    assertEquals(b.compareTo(b) == 0, true);
}
 
Example #16
Source File: TCKZonedDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE - 1;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
    ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
}
 
Example #17
Source File: TCKYear.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_factory_int_singleton() {
    for (int i = -4; i <= 2104; i++) {
        Year test = Year.of(i);
        assertEquals(test.getValue(), i);
        assertEquals(Year.of(i), test);
    }
}
 
Example #18
Source File: TCKMinguoChronology.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="prolepticYear")
public void test_isLeapYear(int eraValue, Era  era, int yearOfEra, int expectedProlepticYear, boolean isLeapYear) {
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), isLeapYear);
    assertEquals(MinguoChronology.INSTANCE.isLeapYear(expectedProlepticYear), Year.of(expectedProlepticYear + YDIFF).isLeap());

    MinguoDate minguo = MinguoDate.now();
    minguo = minguo.with(ChronoField.YEAR, expectedProlepticYear).with(ChronoField.MONTH_OF_YEAR, 2);
    if (isLeapYear) {
        assertEquals(minguo.lengthOfMonth(), 29);
    } else {
        assertEquals(minguo.lengthOfMonth(), 28);
    }
}
 
Example #19
Source File: TestOffsetDateTime_instants.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_tooLow() {
    long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
    int year = Year.MIN_VALUE - 1;
    long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
    Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
    OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
 
Example #20
Source File: TCKOffsetDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_compareTo_max() {
    OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE);
    OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO);  // a is before b due to offset
    assertEquals(a.compareTo(b) < 0, true);
    assertEquals(b.compareTo(a) > 0, true);
    assertEquals(a.compareTo(a) == 0, true);
    assertEquals(b.compareTo(b) == 0, true);
}
 
Example #21
Source File: TCKZoneOffsetTransitionSerialization.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_serialization_unusual2() throws Exception {
    LocalDateTime ldt = LocalDateTime.of(Year.MIN_VALUE, 1, 1, 12, 1, 3);
    ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("+10:02:34"));
    assertSerializable(test);
}
 
Example #22
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plus_Period_invalidTooSmall() {
    MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS);
    LocalDate.of(Year.MIN_VALUE, 1, 1).plus(period);
}
 
Example #23
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plusYears_long_invalidTooLargeMaxAddMin() {
    LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1);
    test.plusYears(Long.MIN_VALUE);
}
 
Example #24
Source File: TCKLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_minusYears_long_invalidTooLargeMaxAddMin() {
    LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1);
    test.minusYears(Long.MIN_VALUE);
}
 
Example #25
Source File: TCKLocalDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plusYears_long_invalidTooSmall_validInt() {
    LocalDate.of(Year.MIN_VALUE, 1, 1).plusYears(-1);
}
 
Example #26
Source File: TCKYearMonth.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_plusYears_long_big() {
    YearMonth test = YearMonth.of(-40, 6);
    assertEquals(test.plusYears(20L + Year.MAX_VALUE), YearMonth.of((int) (-40L + 20L + Year.MAX_VALUE), 6));
}
 
Example #27
Source File: TCKYearMonth.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plusYears_long_invalidTooLarge() {
    YearMonth test = YearMonth.of(Year.MAX_VALUE, 6);
    test.plusYears(1);
}
 
Example #28
Source File: TCKLocalDate.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions={ArithmeticException.class})
public void test_plusWeeks_invalidMaxMinusMax() {
    LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MAX_VALUE);
}
 
Example #29
Source File: TCKLocalDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void test_minusDays_overflowTooSmall() {
    createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(Long.MAX_VALUE);
}
 
Example #30
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_factory_intsMonth_yearTooLow() {
    YearMonth.of(Year.MIN_VALUE - 1, Month.JANUARY);
}