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