本文整理汇总了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);
}