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


Java ZonedDateTime toOffsetDateTime()用法及代碼示例


ZonedDateTime類的toOffsetDateTime()方法用於將此日期時間轉換為OffsetDateTime,而區域ID被忽略。

用法:

public OffsetDateTime toOffsetDateTime()

參數:此方法不帶任何參數。


返回值:此方法返回表示相同本地日期時間和偏移量的偏移日期時間。

以下示例程序旨在說明toOffsetDateTime()方法:

示例1:

// Java program to demonstrate 
// ZonedDateTime.toOffsetDateTime() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]"); 
  
        // print result 
        System.out.println("ZonedDateTime: " + zoneddatetime); 
  
        // get OffsetDateTime 
        OffsetDateTime value = zoneddatetime.toOffsetDateTime(); 
  
        // print result 
        System.out.println("OffsetDateTime: " + value); 
    } 
}
輸出:
ZonedDateTime: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
OffsetDateTime: 2018-12-06T19:21:12.123+05:30

示例2:

// Java program to demonstrate 
// ZonedDateTime.toOffsetDateTime() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a ZonedDateTime object 
        ZonedDateTime zoneddatetime 
            = ZonedDateTime.parse( 
                "2018-10-25T23:12:31.123+02:00[Europe/Paris]"); 
  
        // print result 
        System.out.println("ZonedDateTime: " + zoneddatetime); 
  
        // get OffsetDateTime 
        OffsetDateTime value = zoneddatetime.toOffsetDateTime(); 
  
        // print result 
        System.out.println("OffsetDateTime: " + value); 
    } 
}
輸出:
ZonedDateTime: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
OffsetDateTime: 2018-10-25T23:12:31.123+02:00

參考: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#toOffsetDateTime()



相關用法


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