>  기사  >  Java  >  Java 9에서 ofInstant() 메소드의 중요성은 무엇입니까?

Java 9에서 ofInstant() 메소드의 중요성은 무엇입니까?

WBOY
WBOY앞으로
2023-09-13 13:57:031035검색

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

Java 9에서는 변환을 위해 ofInstant() 메서드가 도입되었습니다. LocalDate, LocalTimeLocalDateTime 클래스의 정적 메서드입니다. 이 메소드는 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으로 문의하시기 바랍니다. 삭제