Java Code Examples for java.time.Instant#ofEpochSecond()

The following examples show how to use java.time.Instant#ofEpochSecond() . 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: TCKInstant.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@DataProvider(name="with_longTemporalField")
Object[][] data_with_longTemporalField() {
    return new Object[][]{
            {Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, 100, Instant.ofEpochSecond(100, 200), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, 0, Instant.ofEpochSecond(0, 200), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.INSTANT_SECONDS, -100, Instant.ofEpochSecond(-100, 200), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 100, Instant.ofEpochSecond(10, 100), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 0, Instant.ofEpochSecond(10), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 100, Instant.ofEpochSecond(10, 100*1000), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 0, Instant.ofEpochSecond(10), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 100, Instant.ofEpochSecond(10, 100*1000*1000), null},
            {Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 0, Instant.ofEpochSecond(10), null},

            {Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_SECOND, 1000000000L, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_SECOND, 1000000, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_SECOND, 1000, null, DateTimeException.class},

            {Instant.ofEpochSecond(10, 200), ChronoField.SECOND_OF_MINUTE, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.SECOND_OF_DAY, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.OFFSET_SECONDS, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.NANO_OF_DAY, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MINUTE_OF_HOUR, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MINUTE_OF_DAY, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MILLI_OF_DAY, 1, null, DateTimeException.class},
            {Instant.ofEpochSecond(10, 200), ChronoField.MICRO_OF_DAY, 1, null, DateTimeException.class},


    };
}
 
Example 2
Source File: TCKInstant.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(long seconds1, int nanos1, long seconds2, long nanos2, TemporalUnit unit, long expected) {
    Instant i1 = Instant.ofEpochSecond(seconds1, nanos1);
    Instant i2 = Instant.ofEpochSecond(seconds2, nanos2);
    long amount = i2.until(i1, unit);
    assertEquals(amount, -expected);
}
 
Example 3
Source File: TCKLocalTime.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void now_Clock_beforeEpoch() {
    for (int i =-1; i >= -(24 * 60 * 60); i--) {
        Instant instant = Instant.ofEpochSecond(i, 8);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        LocalTime test = LocalTime.now(clock);
        assertEquals(test.getHour(), ((i + 24 * 60 * 60) / (60 * 60)) % 24);
        assertEquals(test.getMinute(), ((i + 24 * 60 * 60) / 60) % 60);
        assertEquals(test.getSecond(), (i + 24 * 60 * 60) % 60);
        assertEquals(test.getNano(), 8);
    }
}
 
Example 4
Source File: TCKLocalDate.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_utc() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        LocalDate test = LocalDate.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
    }
}
 
Example 5
Source File: TCKDuration.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test()
@UseDataProvider("data_durationBetweenInstant")
public void factory_between_TemporalTemporal_Instant(long secs1, int nanos1, long secs2, int nanos2, long expectedSeconds, int expectedNanoOfSecond) {
    Instant start = Instant.ofEpochSecond(secs1, nanos1);
    Instant end = Instant.ofEpochSecond(secs2, nanos2);
    Duration t = Duration.between(start, end);
    assertEquals(t.getSeconds(), expectedSeconds);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 6
Source File: TCKInstant.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="PlusMillis")
public void plusMillis_long_minusOneLess(long seconds, int nanos, long amount, long expectedSeconds, int expectedNanoOfSecond) {
    Instant t = Instant.ofEpochSecond(seconds - 1, nanos);
    t = t.plusMillis(amount);
    assertEquals(t.getEpochSecond(), expectedSeconds - 1);
    assertEquals(t.getNano(), expectedNanoOfSecond);
}
 
Example 7
Source File: TestOffsetDateTime_instants.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void factory_ofInstant_allSecsInDay() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_PONE);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
        assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
        assertEquals(test.getMinute(), (i / 60) % 60);
        assertEquals(test.getSecond(), i % 60);
    }
}
 
Example 8
Source File: TCKInstant.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void plus_Duration_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.plus(Duration.ofSeconds(0, 1));
}
 
Example 9
Source File: TCKInstant.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=ArithmeticException.class)
public void plusSeconds_long_overflowTooBig() {
    Instant t = Instant.ofEpochSecond(1, 0);
    t.plusSeconds(Long.MAX_VALUE);
}
 
Example 10
Source File: TestOffsetDateTime_instants.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_ofInstant_maxInstantWithMaxOffset() {
    Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
    OffsetDateTime.ofInstant(instant, OFFSET_MAX);
}
 
Example 11
Source File: TCKInstant.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void test_isBefore_ObjectNull() {
    Instant a = Instant.ofEpochSecond(0L, 0);
    a.isBefore(null);
}
 
Example 12
Source File: TCKInstant.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void plus_Duration_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.plus(Duration.ofSeconds(0, 1));
}
 
Example 13
Source File: SpannerConverters.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Instant convert(Timestamp timestamp) {
	return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}
 
Example 14
Source File: TCKInstantPrinterParser.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider="printGrouped")
public void test_print_grouped(long instantSecs, int nano, String expected) {
    Instant instant = Instant.ofEpochSecond(instantSecs, nano);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendInstant().toFormatter();
    assertEquals(f.format(instant), expected);
}
 
Example 15
Source File: TCKInstant.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void factory_seconds_long_long_tooBig() {
    Instant.ofEpochSecond(MAX_SECOND, 1000000000);
}
 
Example 16
Source File: TCKInstant.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void plusMillis_long_overflowTooSmall() {
    Instant t = Instant.ofEpochSecond(MIN_SECOND, 0);
    t.plusMillis(-1);
}
 
Example 17
Source File: TCKInstant.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void plus_Duration_overflowTooBig() {
    Instant i = Instant.ofEpochSecond(MAX_SECOND, 999999999);
    i.plus(Duration.ofSeconds(0, 1));
}
 
Example 18
Source File: TCKInstant.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void plus_Duration_overflowTooSmall() {
    Instant i = Instant.ofEpochSecond(MIN_SECOND);
    i.plus(Duration.ofSeconds(-1, 999999999));
}
 
Example 19
Source File: TimestampTest.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void plus3() {
    Instant time = Instant.ofEpochSecond(100, 750000000);
    Instant newTime = time.plus(TimeDuration.ofSeconds(5.750));
    assertThat(newTime, equalTo(Instant.ofEpochSecond(106, 500000000)));
}
 
Example 20
Source File: TCKDuration.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void factory_between__TemporalTemporal_endNull() {
    Instant start = Instant.ofEpochSecond(1);
    Duration.between(start, null);
}