Java中的Clock tick()方法
可以使用tick()Java中Clock类中的方法在所需的时间范围内舍入基本时钟的瞬间。此方法需要两个参数,即基本时钟和滴答的持续时间。同样,返回在所需持续时间内四舍五入的基本时钟时刻。
演示此的程序如下所示-
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
Clock bClock = Clock.systemDefaultZone();
Instant i = bClock.instant();
System.out.println("The Instant of the base clock is: " + i);
Clock c1 = Clock.tick(bClock, Duration.ofSeconds(45));
System.out.println("Instant of the first clock with duration 45 seconds is: " + c1.instant());
Clock c2 = Clock.tick(bClock, Duration.ofHours(45));
System.out.println("Instant of the first clock with duration 45 hours is: " + c2.instant());
Clock c3 = Clock.tick(bClock, Duration.ofDays(45));
System.out.println("Instant of the first clock with duration 45 days is: " + c3.instant());
}
}输出结果
The Instant of the base clock is: 2019-02-06T12:26:22.488Z Instant of the first clock with duration 45 seconds is: 2019-02-06T12:26:15Z Instant of the first clock with duration 45 hours is: 2019-02-05T12:00:00Z Instant of the first clock with duration 45 days is: 2019-01-14T00:00:00Z
现在让我们了解上面的程序。
使用方法将基本时钟的时刻四舍五入至所需的持续时间tick()。然后使用instant()方法显示。演示这的代码片段如下-
Clock bClock = Clock.systemDefaultZone();
Instant i = bClock.instant();
System.out.println("The Instant of the base clock is: " + i);
Clock c1 = Clock.tick(bClock, Duration.ofSeconds(45));
System.out.println("Instant of the first clock with duration 45 seconds is: " + c1.instant());
Clock c2 = Clock.tick(bClock, Duration.ofHours(45));
System.out.println("Instant of the first clock with duration 45 hours is: " + c2.instant());
Clock c3 = Clock.tick(bClock, Duration.ofDays(45));
System.out.println("Instant of the first clock with duration 45 days is: " + c3.instant());热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿