java.time.Instant.atZone() 方法示例



描述

java.time.Instant.atZone(ZoneId zone) 方法將此例項與時區結合,建立 ZonedDateTime。

宣告

以下是對 java.time.Instant.atZone(ZoneId zone) 方法的宣告。

public ZonedDateTime atZone(ZoneId zone)

引數

zone - 要與之結合的區域,非空。

返回值

由此例項和指定區域形成的區域化日期時間,非空。

異常

DateTimeException - 如果結果超出了支援範圍。

示例

以下示例展示了 java.time.Instant.atZone(ZoneId zone) 方法的用法。

package com.tutorialspoint;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Set;

public class InstantDemo {
   public static void main(String[] args) {

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      System.out.println(instant);  
      
      Set<String> zones = ZoneId.getAvailableZoneIds();
      
      ZoneId zone = ZoneId.of(zones.iterator().next());
      
      ZonedDateTime  date = instant.atZone(zone);
      System.out.println(date);  
   }
}

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

2017-02-03T10:37:30Z
2017-02-03T13:37:30+03:00[Asia/Aden]
廣告
© . All rights reserved.