本文整理匯總了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();
}
示例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);
}
示例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);
}