Java Code Examples for java.time.Year

The following examples show how to use java.time.Year. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: jdk8u_jdk   Source File: TCKYear.java    License: 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 2
Source Project: jdk8u-jdk   Source File: TCKYear.java    License: 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 3
Source Project: openjdk-jdk8u   Source File: TCKYear.java    License: 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 4
@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 5
Source Project: openjdk-8   Source File: TCKYear.java    License: 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 6
Source Project: openjdk-8-source   Source File: TCKYear.java    License: 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 7
@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 8
Source Project: hottub   Source File: TCKYear.java    License: 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 9
@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 10
@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 11
@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 12
Source Project: openjdk-jdk9   Source File: TCKYear.java    License: 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 13
Source Project: j2objc   Source File: TCKYear.java    License: 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 14
Source Project: TencentKona-8   Source File: TCKYear.java    License: 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 15
Source Project: jdk8u-jdk   Source File: TCKYear.java    License: 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 16
Source Project: jdk8u_jdk   Source File: TCKZonedDateTime.java    License: 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
@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 18
Source Project: openjdk-8   Source File: TCKZonedDateTime.java    License: 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 19
Source Project: hottub   Source File: TCKOffsetDateTime.java    License: 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 20
Source Project: jdk8u-jdk   Source File: TCKYear.java    License: 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 21
Source Project: jdk8u-jdk   Source File: TCKLocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_minusYears_long_invalidTooSmall() {
    LocalDate.of(Year.MIN_VALUE, 1, 1).minusYears(1);
}
 
Example 22
Source Project: openjdk-jdk8u   Source File: TCKYearMonth.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plusYears_long_invalidTooSmall() {
    YearMonth test = YearMonth.of(Year.MIN_VALUE, 6);
    test.plusYears(-1);
}
 
Example 23
Source Project: openjdk-jdk8u   Source File: TCKLocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_withYear_int_invalid() {
    TEST_2007_07_15.withYear(Year.MIN_VALUE - 1);
}
 
Example 24
Source Project: tutorials   Source File: CustomResponseWithBuilderController.java    License: MIT License 4 votes vote down vote up
private int currentYear() {
    return Year.now()
        .getValue();
}
 
Example 25
Source Project: j2objc   Source File: TCKYear.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected=NullPointerException.class)
public void test_compareTo_nullYear() {
    Year doy = null;
    Year test = Year.of(1);
    test.compareTo(doy);
}
 
Example 26
Source Project: hottub   Source File: TCKLocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions={ArithmeticException.class})
public void test_plusWeeks_invalidMaxMinusMin() {
    LocalDate.of(Year.MAX_VALUE, 12, 25).plusWeeks(Long.MIN_VALUE);
}
 
Example 27
Source Project: jdk8u-jdk   Source File: TCKLocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_plusWeeks_minimum() {
    LocalDate t = LocalDate.of(Year.MIN_VALUE, 1, 8).plusWeeks(-1);
    LocalDate expected = LocalDate.of(Year.MIN_VALUE, 1, 1);
    assertEquals(t, expected);
}
 
Example 28
Source Project: openjdk-jdk8u   Source File: TCKLocalDate.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void test_plusMonths_long_invalidTooLargeMaxAddMax() {
    LocalDate test = LocalDate.of(Year.MAX_VALUE, 12, 1);
    test.plusMonths(Long.MAX_VALUE);
}
 
Example 29
Source Project: TencentKona-8   Source File: TCKYear.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_until_convertedType() {
    Year start = Year.of(2010);
    YearMonth end = start.plusYears(2).atMonth(Month.APRIL);
    assertEquals(start.until(end, YEARS), 2);
}
 
Example 30
@Test(expectedExceptions=DateTimeException.class)
public void test_plusMonths_int_invalidTooSmall() {
    createDateMidnight(Year.MIN_VALUE, 1, 1).plusMonths(-1);
}