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


Java Instant atOffset()用法及代碼示例


Instant類的atOffset(ZoneOffset offset)方法用於將此瞬間與偏移量結合在一起以創建OffsetDateTime對象。此方法將ZoneOffset作為參數返回一個OffsetDateTime對象,並且此OffsetDataTime對象是從此時刻開始,與UTC /格林威治指定的偏移量而形成的。如果瞬時值太大而無法適應偏移日期時間,則該方法將引發異常。此方法與OffsetDateTime.ofInstant(this,offset)相同。

用法:

public OffsetDateTime atOffset(ZoneOffset offset)

參數:
此方法接受一個參數偏移量(即ZoneOffset)以與此即時對象組合。不能為空


返回值:此方法返回從此瞬間和指定的ZoneOffset形成的偏移日期時間。

異常:如果瞬間過大而無法容納偏移日期時間,則此方法將引發DateTimeException。

以下示例程序旨在說明Instant.atOffset()方法:
示例1:

// Java program to demonstrate 
// Instant.atOffset() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        // print Instant Value 
        System.out.println("Instant: "
                           + instant); 
  
        // create a ZoneOffset object 
        // with 7200 sec means 2 hours 
        ZoneOffset offset = ZoneOffset.ofTotalSeconds(7200); 
  
        // apply atOffset method to combine ZoneOffset 
        // to this instant 
        OffsetDateTime offsetDate = instant.atOffset(offset); 
  
        // print results 
        System.out.println("Offset Date and Time: "
                           + offsetDate); 
    } 
}
輸出:
Instant: 2018-10-20T16:55:30Z
Offset Date and Time: 2018-10-20T18:55:30+02:00

示例2:

// Java program to demonstrate 
// Instant.atOffset() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.parse("2018-10-20T16:55:30.00Z"); 
  
        // print Instant Value 
        System.out.println("Instant: "
                           + instant); 
  
        // create a ZoneOffset object 
        // with 3 hours 45 minutes 
        ZoneOffset offset 
            = ZoneOffset.ofHoursMinutes(3, 45); 
  
        // apply atOffset method to combine ZoneOffset 
        // to this instant 
        OffsetDateTime offsetDate 
            = instant.atOffset(offset); 
  
        // print results 
        System.out.println("Offset Date and Time: "
                           + offsetDate); 
    } 
}
輸出:
Instant: 2018-10-20T16:55:30Z
Offset Date and Time: 2018-10-20T20:40:30+03:45

示例3:

// Java program to demonstrate 
// Instant.atOffset() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an instance object 
        Instant instant 
            = Instant.now(); 
  
        // print Instant Value 
        System.out.println("Instant: "
                           + instant); 
  
        // create a ZoneOffset Object 
        // with 9 hours 45 minutes 30 second 
        ZoneOffset offset 
            = ZoneOffset 
                  .ofHoursMinutesSeconds(9, 45, 30); 
  
        // apply atOffset method to 
        // combine ZoneOffset to this instant 
        OffsetDateTime offsetDate 
            = instant.atOffset(offset); 
  
        // print results 
        System.out.println("Offset Date and Time: "
                           + offsetDate); 
    } 
}
輸出:
Instant: 2018-11-22T08:22:19.846Z
Offset Date and Time: 2018-11-22T18:07:49.846+09:45:30

參考: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#atOffset(java.time.ZoneOffset)



相關用法


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