📜  Java的.time.Clock类在Java中

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

Java的.time.Clock类在Java中

Java Clock 类存在于Java.time包中。它是在Java 8 中引入的,并使用时区提供对当前时刻、日期和时间的访问。

Clock 类的使用不是强制性的,因为所有日期时间类也有一个 now() 方法,该方法使用默认时区中的系统时钟。使用 Clock 类的主要目的是在需要的地方插入备用时钟。应用程序使用对象而不是静态方法来获取当前时间。这使得测试变得容易。我们可以将 Clock 作为参数传递给需要当前时刻的方法。

宣言:

public abstract class Clock extends Object

它是一个抽象类,所以我们不能实例化它,但是我们可以使用几个静态方法来访问它的实例。

systemUTC() 方法:



public static Clock systemUTC()

此方法返回 UTC 时区中当前时刻的 Clock 实例。当您需要没有日期或时间的当前时刻时,这是最好的。

Java
// Java program for creating instance of Clock
import java.time.Clock;
  
public class GFG {
      
    // main method
    public static void main(String[] args) {
          
        // creating a Clock instance using 
        // systemUTC() method of Clock class
        Clock clock = Clock.systemUTC();
          
        // getting the current instant defined by clock
        System.out.println("UTC time = " + clock.instant());
    }
}


Java
// Java program for creating instance of Clock
  
import java.time.Clock;
  
public class GFG {
  
    // main method
    public static void main(String[] args)
    {
  
        // creating a Clock instance using
        // systemDefaultZone() method of Clock class
        Clock clock = Clock.systemDefaultZone();
  
        // it wil print "SystemClock[Asia/Calcutta]" for me.
        // The output may be different because of server
        // system clock.
        System.out.println(clock);
  
        // printing zone of clock instance
        // it will print "Time zone : Asia/Calcutta" for me.
        System.out.println("Time Zone : "
                           + clock.getZone());
    }
}


输出
UTC time = 2021-02-07T16:16:43.863267Z

systemDefaultZone() 方法

public static Clock systemDefaultZone()

此方法使用系统的默认时区返回当前时刻的 Clock 实例。

Java

// Java program for creating instance of Clock
  
import java.time.Clock;
  
public class GFG {
  
    // main method
    public static void main(String[] args)
    {
  
        // creating a Clock instance using
        // systemDefaultZone() method of Clock class
        Clock clock = Clock.systemDefaultZone();
  
        // it wil print "SystemClock[Asia/Calcutta]" for me.
        // The output may be different because of server
        // system clock.
        System.out.println(clock);
  
        // printing zone of clock instance
        // it will print "Time zone : Asia/Calcutta" for me.
        System.out.println("Time Zone : "
                           + clock.getZone());
    }
}
输出
SystemClock[Etc/UTC]
Time Zone : Etc/UTC

方法

MethodDescription
fixed(Instant fixedInstant, ZoneId zone)Used to get a Clock which always returns the same instant.
getZone()Returns the time zone of the given clock.
instant()Used to get current instant of the clock.
millis()Returns current millisecond instant of the clock. 
offset(Clock baseClock, Duration offsetDuration)Returns a Clock that returns instant from the specified clock with the specified duration added. 
system(ZoneId zone)Returns a Clock object of current instant for the specified zone id.
systemDefaultZone()Returns a Clock instance of the current instant using the default time zone of the system.
systemUTC()     Returns a Clock instance of current instant in UTC time zone.
tick(Clock baseClock, Duration tickDuration)Returns instants from the specified base clock truncated to the nearest occurrence of the specified duration.
tickMinutes(ZoneId zone)Returns a Clock object of current instant ticking in whole minutes for the given time zone.
tickSeconds(Zoneid zone)Returns a Clock object of current instant ticking in whole seconds for the given time zone.
withZone(ZoneId zone)Create a copy of the clock with the different time zone.