java clock计时_Java Clock类| offset()方法与示例

java clock计时

Clock Class offset()方法 (Clock Class offset() method)

  • offset() method is available in java.time package.

    offset()方法在java.time包中可用。

  • offset() method is used to generate a new Clock from the given base clock with added the given Duration.

    offset()方法用于根据给定的基本时钟加上给定的Duration来生成新Clock。

  • offset() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.

    offset()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出错。

  • offset() method does not throw an exception at the time of representing Clock.

    offset()方法在表示Clock时不会引发异常。

Syntax:

句法:

    public static Clock offset(Clock base_cl, Duration off);

Parameter(s):

参数:

  • Clock base_cl – represents the base clock in which to add the given duration.

    Clock base_cl –表示要添加给定持续时间的基本时钟。

  • Duration off – represents the offset to add to the given base clock (base_cl).

    持续时间关闭 –表示要添加到给定基本时钟(base_cl)的偏移量。

Return value:

返回值:

The return type of this method is Clock, it returns the Clock object that is available with added the given offset.

此方法的返回类型为Clock ,它返回添加了给定偏移量后可用的Clock对象。

Example:

例:

// Java program to demonstrate the example 
// of offset(Clock base_cl, Duration off) method of Clock

import java.time.*;

public class OffsetOfClock {
    public static void main(String args[]) {
        // Instantiates two ZoneId for Accra and Asmara
        ZoneId zone_1 = ZoneId.of("Africa/Accra");
        ZoneId zone_2 = ZoneId.of("Africa/Asmara");

        // Initialize two Clock objects 
        // and one Duration
        Clock cl1 = Clock.system(zone_1);
        Clock cl2 = Clock.system(zone_2);
        Duration offset = Duration.ofMinutes(20);

        // generates a new Clock from the
        // given clock(cl1) with the given duration added
        Clock cl_offset = cl1.offset(cl1, offset);

        // Display cl1 and cl_offset instant
        System.out.println("cl1.instant(): " + cl1.instant());
        System.out.println("cl_offset.instant(): " + cl_offset.instant());

        System.out.println();

        // generates a new Clock from the
        // given clock(cl2) with the given duration added
        cl_offset = cl2.offset(cl2, offset);

        // Display cl2 and cl_offset instant
        System.out.println("cl2.instant(): " + cl2.instant());
        System.out.println("cl_offset.instant(): " + cl_offset.instant());
    }
}

Output

输出量

cl1.instant(): 2020-05-13T19:04:36.242559Z
cl_offset.instant(): 2020-05-13T19:24:36.322896Z

cl2.instant(): 2020-05-13T19:04:36.324623Z
cl_offset.instant(): 2020-05-13T19:24:36.327267Z


翻译自: https://www.includehelp.com/java/clock-offset-method-with-example.aspx

java clock计时

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值