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



描述

java.time.ZonedDateTime.ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) 方法從透過合併本地日期時間和偏移量形成的瞬態時間獲取 ZonedDateTime 例項。

宣告

以下是用於 java.time.ZonedDateTime.ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) 方法的宣告。

public static ZonedDateTime ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone)

引數

  • localDateTime − 非空的本地日期時間。

  • offset − 非空的區域偏移量。

  • zone − 時區,可以是偏移量,非空。

返回值

時區的日期時間,非空。

示例

以下示例展示瞭如何使用 java.time.ZonedDateTime.ofInstant(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) 方法。

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
 
      ZonedDateTime date = ZonedDateTime.ofInstant(LocalDateTime.now(),ZoneOffset.UTC, ZoneId.systemDefault() );
      System.out.println(date);  
   }
}

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

2017-03-28T19:30:59.269+05:30[Asia/Calcutta]
廣告
© . All rights reserved.