首页  >  文章  >  Java  >  在Java 9中,ofInstant()方法的重要性是什么?

在Java 9中,ofInstant()方法的重要性是什么?

WBOY
WBOY转载
2023-09-13 13:57:031035浏览

在Java 9中,ofInstant()方法的重要性是什么?

在 Java 9 中,引入了 ofInstant() 方法进行转换。它是 LocalDateLocalTimeLocalDateTime 类的静态方法。该方法将java.time.Instant对象转换为LocalDate,需要java.time.ZoneId形式的时区。

语法

<strong>public static LocalTime ofInstant(Instant instant, ZoneId zone)
public static LocalDate ofInstant(Instant instant, ZoneId zone)
public static LocalDateTime ofInstant(Instant instant, ZoneId zone)</strong>

示例

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Instant;
import java.time.ZoneId;

public class OfInstantMethodTest {
   public static void main(String args[]) {
      <strong>Instant </strong>instant = Instant.<strong>parse</strong>("2020-02-05T02:35:15.245Z");
      System.out.println("Instant: " + instant);

      <strong>LocalDate </strong>ld = LocalDate.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata"));
      System.out.println("LocalDate: " + ld);

      <strong>LocalTime </strong>lt = LocalTime.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata"));
      System.out.println("LocalTime: " + lt);

      <strong>LocalDateTime </strong>ldt = LocalDateTime.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata"));
      System.out.println("LocalDateTime: " + ldt);
   }
}

输出

<strong>Instant: 2020-02-05T02:35:15.245Z
LocalDate: 2020-02-05
LocalTime: 08:05:15.245
LocalDateTime: 2020-02-05T08:05:15.245</strong>

以上是在Java 9中,ofInstant()方法的重要性是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除