In this post I am going to show exactly how to convert Instant to Date and vice versa.
I will use code examples to make things clear!
Let’s get started!
Java Instant to Date
Instant
class is designed to represent specific point in time with nanosecond precision in coordinated universal time (UTC).
There may be scenarios in your project where you may want to convert Instant to Date for legacy use cases.
In below example we create an Instant object and pass the object as an argument to Date.from()
|
|
Output:
|
|
Java Date to Instant
Let us see the scenario where we need to convert date to Instant.
This is most likely used scenario in your project because we are moving from legacy to Instant which is recommended.
Just call toInstant()
method in Date object to get Instant.
Checkout below example as shown below
|
|
|
|