Timestamp to Instant in Java

user123475 picture user123475 · Jul 14, 2016 · Viewed 23.1k times · Source

I have a pojo which has a filed type as Instant. I want to set the Instant getting it from TimsStamp. Would it be possible?

For example: I have a java.sql.Timestamp which I want to convert to java.time.Instant. Would it be possible?

Answer

Dipak Prajapati picture Dipak Prajapati · Jul 14, 2016

we have ready have existing methods which covert Timestamp to Instant and vice versa.

    Instant instant = Instant.now(); // get The current time in instant object
    Timestamp t=java.sql.Timestamp.from(instant); // Convert instant to Timestamp
    Instant anotherInstant=t.toInstant();         // Convert Timestamp to Instant