java.time.Instant.atOffset()方法

java.time.Instant.atOffset(ZoneOffset offset)方法將此瞬間與偏移量組合在一起以創建OffsetDateTime

聲明

以下是java.time.Instant.atOffset(ZoneOffset offset)方法的聲明。

public OffsetDateTime atOffset(ZoneOffset offset)

參數

  • offset - 要與之結合的偏移量,而不是null。

返回值

從此瞬間和指定的偏移量形成的偏移日期時間,不為空。

異常

DateTimeException - 如果結果超出支持的範圍。

示例

以下示例顯示了java.time.Instant.atOffset(ZoneOffset offset)方法的用法。

package com.zaixian;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

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

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      System.out.println(instant);

      ZoneOffset offset = ZoneOffset.ofHours(5);

      OffsetDateTime  date = instant.atOffset(offset);
      System.out.println(date);
   }
}

編譯並運行上面的程式,這將產生以下結果 -

2017-02-03T10:37:30Z
2017-02-03T15:37:30+05:00

上一篇: java.time.Instant類 下一篇: java.time.LocalDate類