Home > Java > javaTutorial > body text

What is the importance of ofInstant() method in Java 9?

WBOY
Release: 2023-09-13 13:57:03
forward
1057 people have browsed it

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

In Java 9, the ofInstant() method was introduced for conversion. It is a static method of the LocalDate, LocalTime and LocalDateTime classes. This method converts the java.time.Instant object to LocalDate, requiring a time zone in the form of java.time.ZoneId.

Syntax

<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>
Copy after login

Example

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);
   }
}
Copy after login

Output

<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>
Copy after login

The above is the detailed content of What is the importance of ofInstant() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!