Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp to Instant in Java [duplicate]

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?

like image 750
user123475 Avatar asked Jul 14 '16 03:07

user123475


People also ask

What is toinstant() method of timestamp class in Java?

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

How to convert localdate to instant and timestamp in Java?

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.

How to get the timestamp after setting Nanos in Java?

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); } }

How do I get the current time in an instant object?

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.


1 Answers

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
like image 53
Dipak Prajapati Avatar answered Nov 02 '22 21:11

Dipak Prajapati