Timestamp to Instant in Java [duplicate]
Asked Answered
D

2

12

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?

Draughtboard answered 14/7, 2016 at 3:47 Comment(2)
can you able to provide more description or code?Vassal
for example: I have a java.sql.Timestamp which I want to convert to java.time.Instant. Would it be possible?Draughtboard
V
18

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
Vassal answered 14/7, 2016 at 4:27 Comment(3)
I already have Timestamp object like 2016-07-14 09:00:0.0. I want this to get converted in to Instant objectDraughtboard
timestampObject.toInstant(); we have method toInstant() in Timestamp class. and you can also see the 3 line of codeVassal
This instant is the instance of joda time not the java time so what you mentioned here did not work for meDraughtboard
B
0

Check this

public Instant toInstant()

Converts this Timestamp object to an Instant. The conversion creates an Instant that represents the same point on the time-line as this Timestamp.

Betjeman answered 14/7, 2016 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.