Converting Date instance to Instant instance

In this post under DateTime, I will show how to convert an instance of “Date” class to “Instant” instance.

Below is the code to that will do the conversion.


    Date date = new Date();
    Instant instant = date.toInstant();

As shown in the above code snippet, first we create a “Date” instance and store in “date” variable.

At next line, we call the “toInstant” method on the “date” variable.

This method will create an “Instant” instance representing the same date and time stored in “date” variable.

In this way we can convert “Date” instance to “Instant” instance.

Leave a Reply