本文整理汇总了Java中java.time.Clock类的典型用法代码示例。如果您正苦于以下问题:Java Clock类的具体用法?Java Clock怎么用?Java Clock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Clock类属于java.time包,在下文中一共展示了Clock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: now_Clock_allSecsInDay_utc
import java.time.Clock; //导入依赖的package包/类
@Test
public void now_Clock_allSecsInDay_utc() {
for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
ZonedDateTime test = ZonedDateTime.now(clock);
assertEquals(test.getYear(), 1970);
assertEquals(test.getMonth(), Month.JANUARY);
assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
assertEquals(test.getHour(), (i / (60 * 60)) % 24);
assertEquals(test.getMinute(), (i / 60) % 60);
assertEquals(test.getSecond(), i % 60);
assertEquals(test.getNano(), 123456789);
assertEquals(test.getOffset(), ZoneOffset.UTC);
assertEquals(test.getZone(), ZoneOffset.UTC);
}
}
示例2: givenMultipleCacheFilesInCacheDirectory_allReadInOrder
import java.time.Clock; //导入依赖的package包/类
@Test
public void
givenMultipleCacheFilesInCacheDirectory_allReadInOrder() throws Exception {
underlyingEventStore.write(stream_1, singletonList(event_1));
Position firstPosition = saveAllToCache(getCacheFile("cache_1.gz"));
underlyingEventStore.write(stream_1, singletonList(event_1));
Position secondPosition = saveAllToCache(getCacheFile("cache_2.gz"), firstPosition);
underlyingEventStore.write(stream_1, singletonList(event_1));
Position thirdPosition = saveAllToCache(getCacheFile("cache_3.gz"), secondPosition);
CacheEventReader newCacheEventReader = new CacheEventReader(new JavaInMemoryEventStore(Clock.systemUTC()), CODEC, cacheDirectory, "cache");
List<ResolvedEvent> resolvedEvents = readAllFromEmpty(newCacheEventReader);
assertThat(resolvedEvents.stream().map(ResolvedEvent::position).collect(toList()), contains(firstPosition, secondPosition, thirdPosition));
}
示例3: produceRecords
import java.time.Clock; //导入依赖的package包/类
/**
* Produce randomly generated records into the defined kafka namespace.
*
* @param numberOfRecords how many records to produce
* @param topicName the namespace name to produce into.
* @param partitionId the partition to produce into.
* @return List of ProducedKafkaRecords.
*/
public List<ProducedKafkaRecord<byte[], byte[]>> produceRecords(
final int numberOfRecords,
final String topicName,
final int partitionId
) {
Map<byte[], byte[]> keysAndValues = new HashMap<>();
// Generate random & unique data
for (int x = 0; x < numberOfRecords; x++) {
// Construct key and value
long timeStamp = Clock.systemUTC().millis();
String key = "key" + timeStamp;
String value = "value" + timeStamp;
// Add to map
keysAndValues.put(key.getBytes(Charsets.UTF_8), value.getBytes(Charsets.UTF_8));
}
return produceRecords(keysAndValues, topicName, partitionId);
}
示例4: now_Clock_allSecsInDay_offset
import java.time.Clock; //导入依赖的package包/类
@Test
public void now_Clock_allSecsInDay_offset() {
for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE);
OffsetDateTime test = OffsetDateTime.now(clock);
assertEquals(test.getYear(), 1970);
assertEquals(test.getMonth(), Month.JANUARY);
assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
assertEquals(test.getHour(), (i / (60 * 60)) % 24);
assertEquals(test.getMinute(), (i / 60) % 60);
assertEquals(test.getSecond(), i % 60);
assertEquals(test.getNano(), 123456789);
assertEquals(test.getOffset(), OFFSET_PONE);
}
}
示例5: testClock
import java.time.Clock; //导入依赖的package包/类
public static void testClock() throws InterruptedException {
//时钟提供给我们用于访问某个特定 时区的 瞬时时间、日期 和 时间的。
Clock c1 = Clock.systemUTC(); //系统默认UTC时钟(当前瞬时时间 System.currentTimeMillis())
System.out.println(c1.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c2 = Clock.systemDefaultZone(); //系统默认时区时钟(当前瞬时时间)
Clock c31 = Clock.system(ZoneId.of("Europe/Paris")); //巴黎时区
System.out.println(c31.millis()); //每次调用将返回当前瞬时时间(UTC)
Clock c32 = Clock.system(ZoneId.of("Asia/Shanghai"));//上海时区
System.out.println(c32.millis());//每次调用将返回当前瞬时时间(UTC)
Clock c4 = Clock.fixed(Instant.now(), ZoneId.of("Asia/Shanghai"));//固定上海时区时钟
System.out.println(c4.millis());
Thread.sleep(1000);
System.out.println(c4.millis()); //不变 即时钟时钟在那一个点不动
Clock c5 = Clock.offset(c1, Duration.ofSeconds(2)); //相对于系统默认时钟两秒的时钟
System.out.println(c1.millis());
System.out.println(c5.millis());
}
示例6: test__equals
import java.time.Clock; //导入依赖的package包/类
public void test__equals() {
Clock a = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
Clock b = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
assertEquals(a.equals(a), true);
assertEquals(a.equals(b), true);
assertEquals(b.equals(a), true);
assertEquals(b.equals(b), true);
Clock c = Clock.tick(Clock.system(MOSCOW), Duration.ofMillis(500));
assertEquals(a.equals(c), false);
Clock d = Clock.tick(Clock.system(PARIS), Duration.ofMillis(499));
assertEquals(a.equals(d), false);
assertEquals(a.equals(null), false);
assertEquals(a.equals("other type"), false);
assertEquals(a.equals(Clock.systemUTC()), false);
}
示例7: test_millis
import java.time.Clock; //导入依赖的package包/类
public void test_millis() {
Clock system = Clock.systemUTC();
assertEquals(system.getZone(), ZoneOffset.UTC);
for (int i = 0; i < 10000; i++) {
// assume can eventually get these within 10 milliseconds
long instant = system.millis();
long systemMillis = System.currentTimeMillis();
if (systemMillis - instant < 10) {
return; // success
}
}
fail();
}
示例8: now
import java.time.Clock; //导入依赖的package包/类
@Test
public void now() {
MonthDay expected = MonthDay.now(Clock.systemDefaultZone());
MonthDay test = MonthDay.now();
for (int i = 0; i < 100; i++) {
if (expected.equals(test)) {
return;
}
expected = MonthDay.now(Clock.systemDefaultZone());
test = MonthDay.now();
}
assertEquals(test, expected);
}
示例9: registerExtenderCallbacks
import java.time.Clock; //导入依赖的package包/类
@Override
public void registerExtenderCallbacks(IBurpExtenderCallbacks callbacks) {
this.callbacks = callbacks;
this.helpers = callbacks.getHelpers();
callbacks.setExtensionName("ProxyHistoryWebUI");
try {
// ONLY THIS PATTERN WORK FINE. ?? ClassLoader.loadClass() NOT WORK... X(
Class.forName("org.h2.Driver");
appContext = new AppContext(Clock.systemDefaultZone());
appContext.setConsoleOutputWriter(new PrintWriter(callbacks.getStdout(), true));
appContext.setConsoleErrorWriter(new PrintWriter(callbacks.getStderr(), true));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
demoPanel = new DemoPanel(appContext);
callbacks.customizeUiComponent(demoPanel);
callbacks.addSuiteTab(BurpExtender.this);
// register ourselves as an HTTP listener
callbacks.registerProxyListener(BurpExtender.this);
}
});
} catch (Exception e) {
callbacks.printError(Throwables.getStackTraceAsString(e));
e.printStackTrace();
}
}
示例10: initSystemComponents
import java.time.Clock; //导入依赖的package包/类
private void initSystemComponents(final SystemComponents systemComponents) {
client = systemComponents.client();
pluginConfig = systemComponents.pluginConfig();
infoStrategy = systemComponents.infoStrategy();
clientUtil = systemComponents.clientUtil();
zorro = new Zorro(pluginConfig);
clock = Clock.systemDefaultZone();
}
示例11: systemClock
import java.time.Clock; //导入依赖的package包/类
@Bean
public Clock systemClock() {
return Clock.systemDefaultZone();
}
示例12: test_MinguoChronology_dateNow
import java.time.Clock; //导入依赖的package包/类
@Test
public void test_MinguoChronology_dateNow() {
ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
Clock clock = Clock.system(zoneId_paris);
Chronology chrono = Chronology.of("Minguo");
assertEquals(chrono.dateNow(), MinguoChronology.INSTANCE.dateNow());
assertEquals(chrono.dateNow(zoneId_paris), MinguoChronology.INSTANCE.dateNow(zoneId_paris));
assertEquals(chrono.dateNow(clock), MinguoChronology.INSTANCE.dateNow(clock));
}
示例13: test_tickSeconds_ZoneId
import java.time.Clock; //导入依赖的package包/类
public void test_tickSeconds_ZoneId() throws Exception {
Clock test = Clock.tickSeconds(PARIS);
assertEquals(test.getZone(), PARIS);
assertEquals(test.instant().getNano(), 0);
Thread.sleep(100);
assertEquals(test.instant().getNano(), 0);
}
示例14: test
import java.time.Clock; //导入依赖的package包/类
@Test
public void test() throws NoSuchMethodException, SecurityException, SQLException {
PropertyMapperManager mapper = new PropertyMapperManager();
LocalDateTime localDateTime = LocalDateTime.now();
OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime, OffsetDateTime.now().getOffset());
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, Clock.systemDefaultZone().getZone());
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(localDateTime);
LocalDate localDate = localDateTime.toLocalDate();
java.sql.Date date = java.sql.Date.valueOf(localDate);
LocalTime localTime = localDateTime.toLocalTime();
java.sql.Time time = new java.sql.Time(toTime(localTime));
OffsetTime offsetTime = offsetDateTime.toOffsetTime();
assertThat(mapper.getValue(JavaType.of(LocalDateTime.class), newResultSet("getTimestamp", timestamp), 1), is(localDateTime));
assertThat(mapper.getValue(JavaType.of(OffsetDateTime.class), newResultSet("getTimestamp", timestamp), 1), is(offsetDateTime));
assertThat(mapper.getValue(JavaType.of(ZonedDateTime.class), newResultSet("getTimestamp", timestamp), 1), is(zonedDateTime));
assertThat(mapper.getValue(JavaType.of(LocalDate.class), newResultSet("getDate", date), 1), is(localDate));
assertThat(mapper.getValue(JavaType.of(LocalTime.class), newResultSet("getTime", time), 1), is(localTime));
assertThat(mapper.getValue(JavaType.of(OffsetTime.class), newResultSet("getTime", time), 1), is(offsetTime));
assertThat(mapper.getValue(JavaType.of(LocalDateTime.class), newResultSet("getTimestamp", null), 1), is(nullValue()));
assertThat(mapper.getValue(JavaType.of(OffsetDateTime.class), newResultSet("getTimestamp", null), 1), is(nullValue()));
assertThat(mapper.getValue(JavaType.of(ZonedDateTime.class), newResultSet("getTimestamp", null), 1), is(nullValue()));
assertThat(mapper.getValue(JavaType.of(LocalDate.class), newResultSet("getDate", null), 1), is(nullValue()));
assertThat(mapper.getValue(JavaType.of(LocalTime.class), newResultSet("getTime", null), 1), is(nullValue()));
assertThat(mapper.getValue(JavaType.of(OffsetTime.class), newResultSet("getTime", null), 1), is(nullValue()));
}
示例15: now_ZoneId
import java.time.Clock; //导入依赖的package包/类
@Test
public void now_ZoneId() {
ZoneId zone = ZoneId.of("UTC+01:02:03");
ZonedDateTime expected = ZonedDateTime.now(Clock.system(zone));
ZonedDateTime test = ZonedDateTime.now(zone);
for (int i = 0; i < 100; i++) {
if (expected.equals(test)) {
return;
}
expected = ZonedDateTime.now(Clock.system(zone));
test = ZonedDateTime.now(zone);
}
assertEquals(test.truncatedTo(ChronoUnit.SECONDS),
expected.truncatedTo(ChronoUnit.SECONDS));
}