📜  带有示例的Java 8 时钟偏移()方法

📅  最后修改于: 2022-05-13 01:55:32.281000             🧑  作者: Mango

带有示例的Java 8 时钟偏移()方法

Java Clock 类是Java的日期时间 API Java .time.Clock 的一部分。 Java日期时间 API 是从Java版本 8 添加的。

offset() 方法是 Clock 类的静态方法,它返回一个时钟,其瞬间等于作为参数传递的时钟的瞬间和特定的 Offset duration的总和。如果添加的持续时间为正,则返回的时钟表示从基准时钟到指定持续时间之后的时钟瞬间。如果添加的持续时间为负,则返回的时钟早于基准时钟的日期和时间。零持续时间对基本时钟没有任何作用并返回基本时钟。

如果基本时钟是不可变的、线程安全的和可序列化的,那么返回的时钟也是不可变的、线程安全的和可序列化的。

句法:

public static Clock offset(Clock baseClock, Duration offsetDuration)

参数:此方法接受两个强制参数:

  • baseclock – 用于增加持续时间的时钟。它不能是空值。
  • offsetDuration – 使用 baseClock 添加的持续时间。它也不能是空值。

返回值:此方法返回一个时钟,其瞬间等于作为参数传递的时钟瞬间之和和特定偏移持续时间之和。

下面的程序说明了Java.time.Clock 类的 offset(Clock baseClock, Duration offsetDuration) 方法:

程序 1:当偏移量以小时为单位传递时。

// Java program to demonstrate offset()
// method of Clock class
  
import java.time.*;
  
// create class
public class offsetMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // base Clock with default zone
        Clock realClock = Clock.systemDefaultZone();
  
        // print current time
        System.out.println("Real clock instant is "
                           + realClock.instant());
  
        // Creating another clock with offset 0
        Clock clock = Clock.offset(realClock, Duration.ZERO);
  
        // print new clock
        System.out.println("New clock instant"
                           + " with Duration = 0 is "
                           + clock.instant());
  
        // Creating the clock with 24 hours positive offset
        clock = Clock.offset(realClock, Duration.ofHours(24));
  
        // print new clock
        System.out.println("New clock instant"
                           + " with Duration = 24hours is "
                           + clock.instant());
  
        // Creating the clock with 24 hours negative offset
        clock = Clock.offset(realClock, Duration.ofHours(-24));
  
        // print new clock
        System.out.println("New clock instant"
                           + " with Duration = -24hours is "
                           + clock.instant());
    }
}
输出:
Real clock instant is 2018-08-21T09:43:13.519Z
New clock instant with Duration = 0 is 2018-08-21T09:43:13.785Z
New clock instant with Duration = 24hours is 2018-08-22T09:43:13.785Z
New clock instant with Duration = -24hours is 2018-08-20T09:43:13.785Z

程序 2:当偏移量以秒和分钟传递时。

// Java program to demonstrate offset() 
// method of Clock class
  
import java.time.*;
  
// create class
public class offsetMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a Zone Id for Europe/Paris
        ZoneId zoneId = ZoneId.of("Europe/Paris");
  
        // base Clock with default zone
        Clock realClock = Clock.system(zoneId);
  
        // print current time
        System.out.println("Real clock instant is "
                           + realClock.instant());
  
        // Creating the clock with 50 seconds positive offset
        Clock clock = Clock.offset(realClock, Duration.ofSeconds(50));
  
        // print new clock
        System.out.println("Time after 50 second later"
                           + " than real Clock is " + clock.instant());
  
        // Creating the clock with 30 minutes positive offset
        clock = Clock.offset(realClock, Duration.ofMinutes(30));
  
        // print new clock
        System.out.println("Time after 30 minutes later"
                           + " than real Clock is " + clock.instant());
    }
}
输出:
Real clock instant is 2018-08-21T09:43:18.921Z
Time after 50 second later than real Clock is 2018-08-21T09:44:08.969Z
Time after 30 minutes later than real Clock is 2018-08-21T10:13:18.969Z

参考: https: Java/time/Clock.html#offset-java.time.Clock-java.time.Duration-