當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java OffsetTime from()用法及代碼示例


Java中的OffsetTime類的from()方法從時態對象獲取OffsetTime的實例,該對象在方法的參數中傳遞。

用法:

public static OffsetTime from(TemporalAccessor temporal)

參數:此方法接受單個強製性參數temporal,該參數指定要轉換的時間對象,並且不為null。


返回值:它返回偏移時間,而不是null。

以下示例程序旨在說明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

程序2

// 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://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#from-java.time.temporal.TemporalAccessor-



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetTime from() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。