本文整理汇总了Java中java.time.Clock.fixed方法的典型用法代码示例。如果您正苦于以下问题:Java Clock.fixed方法的具体用法?Java Clock.fixed怎么用?Java Clock.fixed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Clock
的用法示例。
在下文中一共展示了Clock.fixed方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: 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);
}
}
示例3: 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);
LocalDateTime test = LocalDateTime.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);
}
}
示例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);
LocalDateTime test = LocalDateTime.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);
}
}
示例5: 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);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
LocalDate test = LocalDate.now(clock);
assertEquals(test.getYear(), 1970);
assertEquals(test.getMonth(), Month.JANUARY);
assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
}
}
示例6: now_Clock
import java.time.Clock; //导入方法依赖的package包/类
@Test
public void now_Clock() {
Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
MonthDay test = MonthDay.now(clock);
assertEquals(test.getMonth(), Month.DECEMBER);
assertEquals(test.getDayOfMonth(), 31);
}
示例7: 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 expected = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(expected, ZoneOffset.UTC);
Instant test = Instant.now(clock);
assertEquals(test, expected);
}
}
示例8: now_Clock
import java.time.Clock; //导入方法依赖的package包/类
@Test
public void now_Clock() {
Instant instant = LocalDateTime.of(2010, 12, 31, 0, 0).toInstant(ZoneOffset.UTC);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
YearMonth test = YearMonth.now(clock);
assertEquals(test.getYear(), 2010);
assertEquals(test.getMonth(), Month.DECEMBER);
}
示例9: now_Clock_allSecsInDay_beforeEpoch
import java.time.Clock; //导入方法依赖的package包/类
@Test
public void now_Clock_allSecsInDay_beforeEpoch() {
LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
for (int i =-1; i >= -(24 * 60 * 60); i--) {
Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
LocalDateTime test = LocalDateTime.now(clock);
assertEquals(test.getYear(), 1969);
assertEquals(test.getMonth(), Month.DECEMBER);
assertEquals(test.getDayOfMonth(), 31);
expected = expected.minusSeconds(1);
assertEquals(test.toLocalTime(), expected);
}
}
示例10: now_Clock_maxYear
import java.time.Clock; //导入方法依赖的package包/类
@Test
public void now_Clock_maxYear() {
Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC);
LocalDate test = LocalDate.now(clock);
assertEquals(test, MAX_DATE);
}
示例11: test_withZone_same
import java.time.Clock; //导入方法依赖的package包/类
public void test_withZone_same() {
Clock test = Clock.fixed(INSTANT, PARIS);
Clock changed = test.withZone(PARIS);
assertSame(test, changed);
}
示例12: now_Clock_tooBig
import java.time.Clock; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void now_Clock_tooBig() {
Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
LocalDate.now(clock);
}
示例13: now_Clock_maxYear
import java.time.Clock; //导入方法依赖的package包/类
@Test
public void now_Clock_maxYear() {
Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC);
LocalDateTime test = LocalDateTime.now(clock);
assertEquals(test, MAX_DATE_TIME);
}
示例14: test_toString
import java.time.Clock; //导入方法依赖的package包/类
public void test_toString() {
Clock test = Clock.fixed(INSTANT, PARIS);
assertEquals(test.toString(), "FixedClock[2008-06-30T09:30:10.000000500Z,Europe/Paris]");
}
示例15: test_withZone
import java.time.Clock; //导入方法依赖的package包/类
public void test_withZone() {
Clock test = Clock.fixed(INSTANT, PARIS);
Clock changed = test.withZone(MOSCOW);
assertEquals(test.getZone(), PARIS);
assertEquals(changed.getZone(), MOSCOW);
}