📜  Java中的 OffsetTime from() 方法及示例

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

Java中的 OffsetTime from() 方法及示例

Java中 OffsetTime 类的from()方法从方法参数中传递的时间对象中获取 OffsetTime 的实例。

句法:

public static OffsetTime from(TemporalAccessor temporal)

参数:此方法接受一个强制参数temporal ,它指定要转换的时间对象,它不为空。

返回值:返回偏移时间,不为空。

下面的程序说明了from()方法:

程序 1:

// Java program to demonstrate the from() method
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Function called to get current time
        OffsetTime time
            = OffsetTime.from(ZonedDateTime.now());
  
        // Prints the current time
        System.out.println("Current-time: "
                           + time);
    }
}
输出:
Current-time: 13:07:59.941Z

方案二

// Java program to demonstrate the from() method
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Function called to get current time
        OffsetTime time
            = OffsetTime.from(ZonedDateTime.now());
  
        // Prints the current time
        System.out.println("Current-time: "
                           + time);
    }
}
输出:
Current-time: 13:08:03.087Z

参考:https: Java/time/OffsetTime.html#from-java.time.temporal.TemporalAccessor-