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?
The toInstant () method of Timestamp class returns an Instant which represents the same point on the time-line as this Timestamp. Timestamp : 2018-09-01 09:01:15.0 Instant Timespan : 2018-09-01T03:31:15Z
The LocalDate represents a date in the format yyyy-MM-dd such as 2019-05-16. The Instant is an instantaneous point on the time-line. The Timestamp is a thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. 1. Find the sample code to convert LocalDate to Instant . 2.
Timestamp after setting nanos : " + ts1); // toInstant () method returns an Instant which represents the same point on the time-line as this Timestamp Instant instant = ts1.toInstant (); System.out.println ("4. Instant Timespan : " + instant); } }
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 Show activity on this post. Converts this Timestamp object to an Instant.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With