Java Code Examples for java.time.LocalTime#of()

The following examples show how to use java.time.LocalTime#of() . 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: TCKLocalDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_plusHours_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(3);
    LocalTime t = LocalTime.of(21, 0);

    for (int i = -50; i < 50; i++) {
        LocalDateTime dt = base.plusHours(i);
        t = t.plusHours(1);

        if (t.getHour() == 0) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 2
Source File: TCKZonedDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="sampleTimes")
public void test_get(int y, int o, int d, int h, int m, int s, int n, ZoneId zone) {
    LocalDate localDate = LocalDate.of(y, o, d);
    LocalTime localTime = LocalTime.of(h, m, s, n);
    LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
    ZoneOffset offset = zone.getRules().getOffset(localDateTime);
    ZonedDateTime a = ZonedDateTime.of(localDateTime, zone);

    assertEquals(a.getYear(), localDate.getYear());
    assertEquals(a.getMonth(), localDate.getMonth());
    assertEquals(a.getDayOfMonth(), localDate.getDayOfMonth());
    assertEquals(a.getDayOfYear(), localDate.getDayOfYear());
    assertEquals(a.getDayOfWeek(), localDate.getDayOfWeek());

    assertEquals(a.getHour(), localTime.getHour());
    assertEquals(a.getMinute(), localTime.getMinute());
    assertEquals(a.getSecond(), localTime.getSecond());
    assertEquals(a.getNano(), localTime.getNano());

    assertEquals(a.toLocalDate(), localDate);
    assertEquals(a.toLocalTime(), localTime);
    assertEquals(a.toLocalDateTime(), localDateTime);
    if (zone instanceof ZoneOffset) {
        assertEquals(a.toString(), localDateTime.toString() + offset.toString());
    } else {
        assertEquals(a.toString(), localDateTime.toString() + offset.toString() + "[" + zone.toString() + "]");
    }
}
 
Example 3
Source File: TestUmmAlQuraChronology.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="localDateTime")
Object[][] data_localDateTime() {
    return new Object[][] {
        {LocalDateTime.of(2012, 2, 29, 2, 7), HijrahChronology.INSTANCE.date(1433, 4, 7), LocalTime.of(2, 7), null},
        {ZonedDateTime.of(2012, 2, 29, 2, 7, 1, 1, ZONE_RIYADH), HijrahChronology.INSTANCE.date(1433, 4, 7), LocalTime.of(2, 7, 1, 1), null},
        {OffsetDateTime.of(2012, 2, 29, 2, 7, 1, 1, OFFSET_PTWO), HijrahChronology.INSTANCE.date(1433, 4, 7), LocalTime.of(2, 7, 1, 1), null},
        {JapaneseDate.of(2012, 2, 29), null, null, DateTimeException.class},
        {ThaiBuddhistDate.of(2012 + 543, 2, 29), null, null, DateTimeException.class},
        {LocalDate.of(2012, 2, 29), null, null, DateTimeException.class},
        {LocalTime.of(20, 30, 29, 0), null, null, DateTimeException.class},
    };
}
 
Example 4
Source File: TCKLocalTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test()
@UseDataProvider("provider_sampleToString")
public void test_toString(int h, int m, int s, int n, String expected) {
    LocalTime t = LocalTime.of(h, m, s, n);
    String str = t.toString();
    assertEquals(str, expected);
}
 
Example 5
Source File: TCKLocalTime.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_toNanoOfDay_fromNanoOfDay_symmetry() {
    LocalTime t = LocalTime.of(0, 0);
    for (int i = 0; i < 1000000; i++) {
        assertEquals(LocalTime.ofNanoOfDay(t.toNanoOfDay()), t);
        t = t.plusNanos(1);
    }
    t = LocalTime.of(0, 0);
    for (int i = 1; i <= 1000000; i++) {
        t = t.minusNanos(1);
        assertEquals(LocalTime.ofNanoOfDay(t.toNanoOfDay()), t);
    }
}
 
Example 6
Source File: TCKLocalTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void test_toNanoOfDay() {
    LocalTime t = LocalTime.of(0, 0);
    for (int i = 0; i < 1000000; i++) {
        assertEquals(t.toNanoOfDay(), i);
        t = t.plusNanos(1);
    }
    t = LocalTime.of(0, 0);
    for (int i = 1; i <= 1000000; i++) {
        t = t.minusNanos(1);
        assertEquals(t.toNanoOfDay(), 24 * 60 * 60 * 1000000000L - i);
    }
}
 
Example 7
Source File: TCKChronoField.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] data_fieldAndAccessor() {
    return new Object[][] {
            {YEAR, LocalDate.of(2000, 2, 29), true, 2000},
            {YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 2000},
            {MONTH_OF_YEAR, LocalDate.of(2000, 2, 29), true, 2},
            {MONTH_OF_YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 2},
            {DAY_OF_MONTH, LocalDate.of(2000, 2, 29), true, 29},
            {DAY_OF_MONTH, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 29},
            {DAY_OF_YEAR, LocalDate.of(2000, 2, 29), true, 60},
            {DAY_OF_YEAR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 60},

            {HOUR_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5},
            {HOUR_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5},

            {MINUTE_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5*60 + 4},
            {MINUTE_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5*60 + 4},
            {MINUTE_OF_HOUR, LocalTime.of(5, 4, 3, 200), true, 4},
            {MINUTE_OF_HOUR, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 4},

            {SECOND_OF_DAY, LocalTime.of(5, 4, 3, 200), true, 5*3600 + 4*60 + 3},
            {SECOND_OF_DAY, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 5*3600 + 4*60 + 3},
            {SECOND_OF_MINUTE, LocalTime.of(5, 4, 3, 200), true, 3},
            {SECOND_OF_MINUTE, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 3},

            {NANO_OF_SECOND, LocalTime.of(5, 4, 3, 200), true, 200},
            {NANO_OF_SECOND, LocalDateTime.of(2000, 2, 29, 5, 4, 3, 200), true, 200},

            {YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
            {MONTH_OF_YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
            {DAY_OF_MONTH, LocalTime.of(5, 4, 3, 200), false, -1},
            {DAY_OF_YEAR, LocalTime.of(5, 4, 3, 200), false, -1},
            {HOUR_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
            {MINUTE_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
            {MINUTE_OF_HOUR, LocalDate.of(2000, 2, 29), false, -1},
            {SECOND_OF_DAY, LocalDate.of(2000, 2, 29), false, -1},
            {SECOND_OF_MINUTE, LocalDate.of(2000, 2, 29), false, -1},
            {NANO_OF_SECOND, LocalDate.of(2000, 2, 29), false, -1},
    };
}
 
Example 8
Source File: TestLocalTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void factory_time_3ints_singletons() {
    for (int i = 0; i < 24; i++) {
        LocalTime test1 = LocalTime.of(i, 0, 0);
        LocalTime test2 = LocalTime.of(i, 0, 0);
        assertSame(test1, test2);
    }
}
 
Example 9
Source File: TCKLocalTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="sampleTimes")
public void test_get(int h, int m, int s, int ns) {
    LocalTime a = LocalTime.of(h, m, s, ns);
    assertEquals(a.getHour(), h);
    assertEquals(a.getMinute(), m);
    assertEquals(a.getSecond(), s);
    assertEquals(a.getNano(), ns);
}
 
Example 10
Source File: TCKLocalTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_time_4ints_hourTooHigh() {
    LocalTime.of(24, 0, 0, 0);
}
 
Example 11
Source File: TCKLocalTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_time_2ints_hourTooLow() {
    LocalTime.of(-1, 0);
}
 
Example 12
Source File: TestLocalDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="sampleTimes")
public void test_getTime(int h, int m, int s, int ns) {
    LocalTime t = LocalTime.of(h, m, s, ns);
    LocalDateTime dt = LocalDateTime.of(LocalDate.of(2011, 7, 30), t);
    assertSame(dt.toLocalTime(), t);
}
 
Example 13
Source File: TCKLocalTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="sampleToString")
public void test_toString(int h, int m, int s, int n, String expected) {
    LocalTime t = LocalTime.of(h, m, s, n);
    String str = t.toString();
    assertEquals(str, expected);
}
 
Example 14
Source File: TCKLocalTimeSerialization.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@BeforeMethod
public void setUp() {
    TEST_12_30_40_987654321 = LocalTime.of(12, 30, 40, 987654321);
}
 
Example 15
Source File: TCKLocalTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void test_atDate() {
    LocalTime t = LocalTime.of(11, 30);
    assertEquals(t.atDate(LocalDate.of(2012, 6, 30)), LocalDateTime.of(2012, 6, 30, 11, 30));
}
 
Example 16
Source File: TCKDateTimeFormatter.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@DataProvider(name="formatWithZoneWithChronology")
Object[][] data_format_withZone_withChronology() {
    YearMonth ym = YearMonth.of(2008, 6);
    LocalDate ld = LocalDate.of(2008, 6, 30);
    LocalTime lt = LocalTime.of(11, 30);
    LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 11, 30);
    OffsetTime ot = OffsetTime.of(LocalTime.of(11, 30), OFFSET_PONE);
    OffsetDateTime odt = OffsetDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), OFFSET_PONE);
    ZonedDateTime zdt = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), ZONE_PARIS);
    ChronoZonedDateTime<ThaiBuddhistDate> thaiZdt = ThaiBuddhistChronology.INSTANCE.zonedDateTime(zdt);
    Instant instant = Instant.ofEpochSecond(3600);
    return new Object[][] {
            {null, null, DayOfWeek.MONDAY, "::::"},
            {null, null, ym, "2008::::ISO"},
            {null, null, ld, "2008::::ISO"},
            {null, null, lt, ":11:::"},
            {null, null, ldt, "2008:11:::ISO"},
            {null, null, ot, ":11:+01:00::"},
            {null, null, odt, "2008:11:+01:00::ISO"},
            {null, null, zdt, "2008:11:+02:00:Europe/Paris:ISO"},
            {null, null, instant, "::::"},

            {IsoChronology.INSTANCE, null, DayOfWeek.MONDAY, "::::ISO"},
            {IsoChronology.INSTANCE, null, ym, "2008::::ISO"},
            {IsoChronology.INSTANCE, null, ld, "2008::::ISO"},
            {IsoChronology.INSTANCE, null, lt, ":11:::ISO"},
            {IsoChronology.INSTANCE, null, ldt, "2008:11:::ISO"},
            {IsoChronology.INSTANCE, null, ot, ":11:+01:00::ISO"},
            {IsoChronology.INSTANCE, null, odt, "2008:11:+01:00::ISO"},
            {IsoChronology.INSTANCE, null, zdt, "2008:11:+02:00:Europe/Paris:ISO"},
            {IsoChronology.INSTANCE, null, instant, "::::ISO"},

            {null, ZONE_PARIS, DayOfWeek.MONDAY, ":::Europe/Paris:"},
            {null, ZONE_PARIS, ym, "2008:::Europe/Paris:ISO"},
            {null, ZONE_PARIS, ld, "2008:::Europe/Paris:ISO"},
            {null, ZONE_PARIS, lt, ":11::Europe/Paris:"},
            {null, ZONE_PARIS, ldt, "2008:11::Europe/Paris:ISO"},
            {null, ZONE_PARIS, ot, ":11:+01:00:Europe/Paris:"},
            {null, ZONE_PARIS, odt, "2008:12:+02:00:Europe/Paris:ISO"},
            {null, ZONE_PARIS, zdt, "2008:11:+02:00:Europe/Paris:ISO"},
            {null, ZONE_PARIS, instant, "1970:02:+01:00:Europe/Paris:ISO"},

            {null, OFFSET_PTHREE, DayOfWeek.MONDAY, ":::+03:00:"},
            {null, OFFSET_PTHREE, ym, "2008:::+03:00:ISO"},
            {null, OFFSET_PTHREE, ld, "2008:::+03:00:ISO"},
            {null, OFFSET_PTHREE, lt, ":11::+03:00:"},
            {null, OFFSET_PTHREE, ldt, "2008:11::+03:00:ISO"},
            {null, OFFSET_PTHREE, ot, null},  // offset and zone clash
            {null, OFFSET_PTHREE, odt, "2008:13:+03:00:+03:00:ISO"},
            {null, OFFSET_PTHREE, zdt, "2008:12:+03:00:+03:00:ISO"},
            {null, OFFSET_PTHREE, instant, "1970:04:+03:00:+03:00:ISO"},

            {ThaiBuddhistChronology.INSTANCE, null, DayOfWeek.MONDAY, null},  // not a complete date
            {ThaiBuddhistChronology.INSTANCE, null, ym, null},  // not a complete date
            {ThaiBuddhistChronology.INSTANCE, null, ld, "2551::::ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, lt, ":11:::ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, ldt, "2551:11:::ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, ot, ":11:+01:00::ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, odt, "2551:11:+01:00::ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, zdt, "2551:11:+02:00:Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, null, instant, "::::ThaiBuddhist"},

            {ThaiBuddhistChronology.INSTANCE, null, DayOfWeek.MONDAY, null},  // not a complete date
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, ym, null},  // not a complete date
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, ld, "2551:::Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, lt, ":11::Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, ldt, "2551:11::Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, ot, ":11:+01:00:Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, odt, "2551:12:+02:00:Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, zdt, "2551:11:+02:00:Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, instant, "2513:02:+01:00:Europe/Paris:ThaiBuddhist"},

            {null, ZONE_PARIS, thaiZdt, "2551:11:+02:00:Europe/Paris:ThaiBuddhist"},
            {ThaiBuddhistChronology.INSTANCE, ZONE_PARIS, thaiZdt, "2551:11:+02:00:Europe/Paris:ThaiBuddhist"},
            {IsoChronology.INSTANCE, ZONE_PARIS, thaiZdt, "2008:11:+02:00:Europe/Paris:ISO"},
    };
}
 
Example 17
Source File: TCKLocalTime.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_time_2ints_minuteTooLow() {
    LocalTime.of(0, -1);
}
 
Example 18
Source File: TCKLocalTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_time_2ints_hourTooLow() {
    LocalTime.of(-1, 0);
}
 
Example 19
Source File: TCKLocalTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_time_3ints_hourTooLow() {
    LocalTime.of(-1, 0, 0);
}
 
Example 20
Source File: TCKLocalTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="sampleTimes")
public void test_equals_false_nano_differs(int h, int m, int s, int n) {
    LocalTime a = LocalTime.of(h, m, s, n);
    LocalTime b = LocalTime.of(h, m, s, n + 1);
    assertEquals(a.equals(b), false);
}