當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。