In this post we’ll see Java programs to convert java.time.Instant to LocalDate, LocalTime and LocalDateTime.
1. An Instant provides an instantaneous point of time in UTC (Coordinated Universal Time) so converting it to LocalDate, LocalDateTime involves converting instant to-
- ZonedDateTime by specifying the ZoneID used.
- OffsetDateTime by specifying the ZoneOffset used.
Once you have an instance of ZonedDateTime or OffsetDateTime you can get LocalDate, LocalTime or LocalDateTime by using methods toLocalDate(), toLocalTime(), toLocalDateTime() respectively.
2. Another way is to use ofInstant()
static method in LocalDate, LocalTime, LocalDateTime classes for
conversion.
Instant to LocalDate, LocalDateTime using ZonedDateTime
public class InsantExample { public static void main(String[] args) { Instant instant = Instant.parse("2019-10-20T02:35:15.245Z"); System.out.println("Instant- " + instant); // Getting ZonedDateTime ZonedDateTime zdt = instant.atZone(ZoneId.of("Europe/Paris")); System.out.println("ZonedDateTime- " + zdt); // Getting LocalDate System.out.println("LocalDate- " + zdt.toLocalDate()); System.out.println("LocalTime- " + zdt.toLocalTime()); System.out.println("LocalDateTime- " + zdt.toLocalDateTime()); } }Output
Instant- 2019-10-20T02:35:15.245Z ZonedDateTime- 2019-10-20T04:35:15.245+02:00[Europe/Paris] LocalDate- 2019-10-20 LocalTime- 04:35:15.245 LocalDateTime- 2019-10-20T04:35:15.245
Instant to LocalTime, LocalDateTime using OffsetDateTime
public class InsantExample { public static void main(String[] args) { Instant instant = Instant.parse("2019-10-20T02:35:15.245Z"); System.out.println("Instant- " + instant); // Getting OffsetDateTime by specifying offset OffsetDateTime odt = instant.atOffset(ZoneOffset.of("+02:00")); System.out.println("OffsetDateTime- " + odt); // Getting LocalDate System.out.println("LocalDate- " + odt.toLocalDate()); System.out.println("LocalTime- " + odt.toLocalTime()); System.out.println("LocalDateTime- " + odt.toLocalDateTime()); } }Output
Instant- 2019-10-20T02:35:15.245Z OffsetDateTime- 2019-10-20T04:35:15.245+02:00 LocalDate- 2019-10-20 LocalTime- 04:35:15.245 LocalDateTime- 2019-10-20T04:35:15.245
Using ofInstant() method
For LocalDateInstant instant = Instant.parse("2019-10-20T02:35:15.245Z"); System.out.println("Instant- " + instant); LocalDate ld = LocalDate.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDate- " + ld);Output
Instant- 2019-10-20T02:35:15.245Z LocalDate- 2019-10-20For LocalTime
Instant instant = Instant.parse("2019-10-20T02:35:15.245Z"); System.out.println("Instant- " + instant); LocalTime lt = LocalTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalTime- " + lt);Output
Instant- 2019-10-20T02:35:15.245Z LocalTime- 08:05:15.245For LocalDateTime
Instant instant = Instant.parse("2019-10-20T02:35:15.245Z"); System.out.println("Instant- " + instant); LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDateTime- " + ldt);Output
Instant- 2019-10-20T02:35:15.245Z LocalDateTime- 2019-10-20T08:05:15.245
That's all for the topic Convert Instant to Java LocalDateTime, LocalTime . If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Program to Convert Date to LocalDate, LocalDateTime
- Convert LocalDateTime to OffsetDateTime in Java
- Convert HTML to PDF in Java Using Flying Saucer, OpenPDF
- JDBC Batch Insert Update Example
- How to Pass Command Line Arguments in Eclipse
- isAlive() And join() Methods in Java
- Spring Bean Definition Inheritance
- Input Split in Hadoop MapReduce
No comments:
Post a Comment