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


Java Clock类代码示例

本文整理汇总了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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:TCKZonedDateTime.java

示例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));
}
 
开发者ID:tim-group,项目名称:tg-eventstore,代码行数:17,代码来源:CachingEventsTest.java

示例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);
}
 
开发者ID:salesforce,项目名称:kafka-junit,代码行数:29,代码来源:KafkaTestUtils.java

示例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);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:TCKOffsetDateTime.java

示例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());
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:18,代码来源:TimeIntroduction.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:19,代码来源:TCKClock_Tick.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKClock_System.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:TCKMonthDay.java

示例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();
    }
}
 
开发者ID:SecureSkyTechnology,项目名称:burpextender-proxyhistory-webui,代码行数:31,代码来源:BurpExtender.java

示例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();
}
 
开发者ID:juxeii,项目名称:dztools,代码行数:9,代码来源:Components.java

示例11: systemClock

import java.time.Clock; //导入依赖的package包/类
@Bean
public Clock systemClock() {
    return Clock.systemDefaultZone();
}
 
开发者ID:hmcts,项目名称:cmc-claim-store,代码行数:5,代码来源:TimeConfiguration.java

示例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));
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:TCKChronology.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:TCKClock_Tick.java

示例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()));

}
 
开发者ID:future-architect,项目名称:uroborosql,代码行数:30,代码来源:DateTimeApiPropertyMapperTest.java

示例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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:16,代码来源:TCKZonedDateTime.java


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