Java Code Examples for java.time.Clock#system()

The following examples show how to use java.time.Clock#system() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeIntroduction.java    From util4j with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 2
Source File: TCKChronology.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_ThaiBuddhistChronology_dateNow() {
    ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
    Clock clock = Clock.system(zoneId_paris);

    Chronology chrono = Chronology.of("ThaiBuddhist");
    assertEquals(chrono.dateNow(), ThaiBuddhistChronology.INSTANCE.dateNow());
    assertEquals(chrono.dateNow(zoneId_paris), ThaiBuddhistChronology.INSTANCE.dateNow(zoneId_paris));
    assertEquals(chrono.dateNow(clock), ThaiBuddhistChronology.INSTANCE.dateNow(clock));
}
 
Example 3
Source File: SystemClockConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    reader.moveDown();
    final ZoneId zone = (ZoneId)context.convertAnother(null, ZoneId.class);
    reader.moveUp();
    return Clock.system(zone);
}
 
Example 4
Source File: TCKChronology.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_JapaneseChronology_dateNow() {
    ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
    Clock clock = Clock.system(zoneId_paris);

    Chronology chrono = Chronology.of("Japanese");
    assertEquals(chrono.dateNow(), JapaneseChronology.INSTANCE.dateNow());
    assertEquals(chrono.dateNow(zoneId_paris), JapaneseChronology.INSTANCE.dateNow(zoneId_paris));
    assertEquals(chrono.dateNow(clock), JapaneseChronology.INSTANCE.dateNow(clock));
}
 
Example 5
Source File: TCKChronology.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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));
}
 
Example 6
Source File: TCKChronology.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test_ThaiBuddhistChronology_dateNow() {
    ZoneId zoneId_paris = ZoneId.of("Europe/Paris");
    Clock clock = Clock.system(zoneId_paris);

    Chronology chrono = Chronology.of("ThaiBuddhist");
    assertEquals(chrono.dateNow(), ThaiBuddhistChronology.INSTANCE.dateNow());
    assertEquals(chrono.dateNow(zoneId_paris), ThaiBuddhistChronology.INSTANCE.dateNow(zoneId_paris));
    assertEquals(chrono.dateNow(clock), ThaiBuddhistChronology.INSTANCE.dateNow(clock));
}
 
Example 7
Source File: TCKClock_System.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void test_zoneId_nullZoneId() {
    Clock.system(null);
}
 
Example 8
Source File: TestClock_System.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void test_toString() {
    Clock test = Clock.system(PARIS);
    assertEquals(test.toString(), "SystemClock[Europe/Paris]");
}
 
Example 9
Source File: TCKClock_System.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void test_system_ZoneId() {
    Clock test = Clock.system(PARIS);
    assertEquals(test.getZone(), PARIS);
}
 
Example 10
Source File: TCKClock_System.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void test_withZone_equal() {
    Clock test = Clock.system(PARIS);
    Clock changed = test.withZone(PARIS);
    assertEquals(changed.getZone(), PARIS);
}
 
Example 11
Source File: TCKClock_Tick.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void test_tick_ClockDuration_zeroDuration() {
    Clock underlying = Clock.system(PARIS);
    Clock test = Clock.tick(underlying, Duration.ZERO);
    assertSame(test, underlying);  // spec says same
}
 
Example 12
Source File: TCKClock_System.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void test_system_ZoneId() {
    Clock test = Clock.system(PARIS);
    assertEquals(test.getZone(), PARIS);
}
 
Example 13
Source File: TestClock_System.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void test_withZone_same() {
    Clock test = Clock.system(PARIS);
    Clock changed = test.withZone(PARIS);
    assertSame(test, changed);
}
 
Example 14
Source File: TCKClock_Tick.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void test_tick_ClockDuration_zeroDuration() {
    Clock underlying = Clock.system(PARIS);
    Clock test = Clock.tick(underlying, Duration.ZERO);
    assertSame(test, underlying);  // spec says same
}
 
Example 15
Source File: TCKClock_Tick.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void test_tick_ClockDuration_1nsDuration() {
    Clock underlying = Clock.system(PARIS);
    Clock test = Clock.tick(underlying, Duration.ofNanos(1));
    assertSame(test, underlying);  // spec says same
}
 
Example 16
Source File: TCKClock_System.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void test_system_ZoneId() {
    Clock test = Clock.system(PARIS);
    assertEquals(test.getZone(), PARIS);
}
 
Example 17
Source File: TestClock_System.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void test_toString() {
    Clock test = Clock.system(PARIS);
    assertEquals(test.toString(), "SystemClock[Europe/Paris]");
}
 
Example 18
Source File: TestClock_System.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void test_withZone_same() {
    Clock test = Clock.system(PARIS);
    Clock changed = test.withZone(PARIS);
    assertSame(test, changed);
}
 
Example 19
Source File: TCKClock_System.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_withZone() {
    Clock test = Clock.system(PARIS);
    Clock changed = test.withZone(MOSCOW);
    assertEquals(test.getZone(), PARIS);
    assertEquals(changed.getZone(), MOSCOW);
}
 
Example 20
Source File: TCKClock_System.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void test_zoneId_nullZoneId() {
    Clock.system(null);
}