11

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?

2
  • can you able to provide more description or code? Jul 14, 2016 at 3:52
  • for example: I have a java.sql.Timestamp which I want to convert to java.time.Instant. Would it be possible?
    – user123475
    Jul 14, 2016 at 4:01

2 Answers 2

17

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
3
  • I already have Timestamp object like 2016-07-14 09:00:0.0. I want this to get converted in to Instant object
    – user123475
    Jul 14, 2016 at 13:04
  • timestampObject.toInstant(); we have method toInstant() in Timestamp class. and you can also see the 3 line of code Jul 15, 2016 at 3:50
  • This instant is the instance of joda time not the java time so what you mentioned here did not work for me
    – user123475
    Jul 22, 2016 at 14:06
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.