java.time.ZonedDateTime.withZoneSameInstant() 方法示例



描述

java.time.ZonedDateTime.withZoneSameInstant(ZoneId zone) 方法返回本日期時間的副本,副本採用不同的時區,但保留相同的即時時間。

宣告

以下是 java.time.ZonedDateTime.withZoneSameInstant(ZoneId zone) 方法的宣告。

public ZonedDateTime withZoneSameInstant(ZoneId zone)

引數

zone − 要更改為的時區,不可為 null。

返回值

基於本日期的 ZonedDateTime,採用所請求的時區,不可為 null。

異常

DateTimeException - 如果年份值無效。

示例

以下示例演示了 java.time.ZonedDateTime.withZoneSameInstant(ZoneId zone) 方法的使用。

package com.tutorialspoint;

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
      
      ZonedDateTime date = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]");
      ZonedDateTime result = date.withZoneSameInstant(ZoneId.of("Z"));
      System.out.println(result);  
   }
}

讓我們編譯並執行以上程式,這將生成以下結果 -

2017-03-28T06:55:38.492Z
廣告