Java Code Examples for java.time.Instant.atOffset()

The following are Jave code examples for showing how to use atOffset() of the java.time.Instant class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: cakes   File: InstantDemo.java   Source Code and License Vote up 6 votes
/**
 * 初始化
 */
@Test
public void testCreate() {
    // 此时获取的是 UTC时区的时间,北京时间需要加上偏移量
    Instant instant = Instant.now();
    System.out.println(instant);

    // 偏移8个小时,东八区
    OffsetDateTime dateTime = instant.atOffset(ZoneOffset.ofHours(8));
    System.out.println(dateTime);

    // 相对1970-1-1 00:00:00,向后偏移1000000毫秒
    Instant instantMilli = Instant.ofEpochMilli(1000000);
    System.out.println(instantMilli);

    // 相对1970-1-1 00:00:00,向后偏移60秒
    Instant instantSecond = Instant.ofEpochSecond(60);
    System.out.println(instantSecond);

}
 
Example 2
Project: jdk8u-jdk   File: TCKInstant.java   Source Code and License Vote up 5 votes
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        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 3
Project: openjdk-jdk10   File: TCKInstant.java   Source Code and License Vote up 5 votes
@Test
public void test_atOffset() {
    for (int i = 0; i < (24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        OffsetDateTime test = instant.atOffset(ZoneOffset.ofHours(1));
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonthValue(), 1);
        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 4
Project: trading4j   File: M30.java   Source Code and License Vote up 4 votes
@Override
public Instant instantOfNextFrame(final Instant current) {
    final OffsetDateTime time = current.atOffset(UTC);
    final int minutes = time.get(MINUTE_OF_HOUR);
    return time.withSecond(0).withNano(0).plusMinutes(30 - (minutes % 30)).toInstant();
}
 
Example 5
Project: trading4j   File: M5.java   Source Code and License Vote up 4 votes
@Override
public Instant instantOfNextFrame(final Instant current) {
    final OffsetDateTime time = current.atOffset(UTC);
    final int minutes = time.get(MINUTE_OF_HOUR);
    return time.withSecond(0).withNano(0).plusMinutes(5 - (minutes % 5)).toInstant();
}
 
Example 6
Project: trading4j   File: M15.java   Source Code and License Vote up 4 votes
@Override
public Instant instantOfNextFrame(final Instant current) {
    final OffsetDateTime time = current.atOffset(UTC);
    final int minutes = time.get(MINUTE_OF_HOUR);
    return time.withSecond(0).withNano(0).plusMinutes(15 - (minutes % 15)).toInstant();
}
 
Example 7
Project: trading4j   File: H1.java   Source Code and License Vote up 4 votes
@Override
public Instant instantOfNextFrame(final Instant current) {
    final OffsetDateTime time = current.atOffset(UTC);
    return time.withSecond(0).withNano(0).withMinute(0).plusHours(1).toInstant();
}