当前位置: 首页>>代码示例>>Java>>正文


Java Clock.instant方法代码示例

本文整理汇总了Java中java.time.Clock.instant方法的典型用法代码示例。如果您正苦于以下问题:Java Clock.instant方法的具体用法?Java Clock.instant怎么用?Java Clock.instant使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.Clock的用法示例。


在下文中一共展示了Clock.instant方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: test_instant

import java.time.Clock; //导入方法依赖的package包/类
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:TCKClock_System.java

示例2: test_tickMinutes_ZoneId

import java.time.Clock; //导入方法依赖的package包/类
public void test_tickMinutes_ZoneId() {
    Clock test = Clock.tickMinutes(PARIS);
    assertEquals(test.getZone(), PARIS);
    Instant instant = test.instant();
    assertEquals(instant.getEpochSecond() % 60, 0);
    assertEquals(instant.getNano(), 0);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:8,代码来源:TCKClock_Tick.java

示例3: testWithOffset

import java.time.Clock; //导入方法依赖的package包/类
static void testWithOffset(String name, long offset, Clock clock)
        throws IllegalAccessException {
    offsetField.set(clock, offset);
    long beforeMillis = System.currentTimeMillis();
    final Instant instant = clock.instant();
    long afterMillis = System.currentTimeMillis();
    long actualOffset = offsetField.getLong(clock);
    long instantMillis = instant.getEpochSecond() * MILLIS_IN_SECOND
            + instant.getNano() / NANOS_IN_MILLI;
    if (instantMillis < beforeMillis || instantMillis > afterMillis) {
        throw new RuntimeException(name
                + ": Invalid instant: " + instant
                + " (~" + instantMillis + "ms)"
                + " when time in millis is in ["
                + beforeMillis + ", " + afterMillis
                + "] and offset in seconds is " + offset);
    }
    Answer isOffLimits = isOffLimits(beforeMillis / MILLIS_IN_SECOND,
            afterMillis / MILLIS_IN_SECOND, offset);
    switch (isOffLimits) {
        case YES:
            if (actualOffset == offset) {
                throw new RuntimeException(name
                        + ": offset was offlimit but was not recomputed "
                        + " when time in millis is in ["
                        + beforeMillis + ", " + afterMillis
                        + "] and offset in seconds was " + offset);
            }
            break;
        case NO:
            if (actualOffset != offset) {
                throw new RuntimeException(name
                        + ": offset was not offlimit but was recomputed.");
            }
            break;
        default:
            break;
    }
    if (distance(actualOffset, instant.getEpochSecond()) >= MAX_OFFSET) {
        throw new RuntimeException(name + ": Actual offset is too far off:"
                + " offset=" + actualOffset
                + "instant.seconds=" + instant.getEpochSecond());
    }
    long adjustment = (instant.getEpochSecond() - actualOffset) * NANOS_IN_SECOND
            + instant.getNano();
    validateAdjustment(name, actualOffset, beforeMillis, afterMillis, adjustment);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:48,代码来源:TestClock_System.java


注:本文中的java.time.Clock.instant方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。