![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of ofInstant() method in Java 9?
In Java 9, the ofInstant() method has introduced for conversion. It is a static method of LocalDate, LocalTime, and LocalDateTime classes. This method converts java.time.Instant object to LocalDate that requires a time zone in the form of java.time.ZoneId.
Syntax
public static LocalTime ofInstant(Instant instant, ZoneId zone) public static LocalDate ofInstant(Instant instant, ZoneId zone) public static LocalDateTime ofInstant(Instant instant, ZoneId zone)
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[]) { Instant instant = Instant.parse("2020-02-05T02:35:15.245Z"); System.out.println("Instant: " + instant); LocalDate ld = LocalDate.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDate: " + ld); LocalTime lt = LocalTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalTime: " + lt); LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDateTime: " + ldt); } }
Output
Instant: 2020-02-05T02:35:15.245Z LocalDate: 2020-02-05 LocalTime: 08:05:15.245 LocalDateTime: 2020-02-05T08:05:15.245
Advertisements