Java : convert long to Timestamp
Asked Answered
M

6

36

I know, how to convert a Timestamp to a long, with the getTime() method.

Is there a method that convert a long to a TimeStamp?

Malines answered 14/5, 2012 at 20:8 Comment(0)
G
64

The constructor is doing that:

Timestamp(long time) 
Geraud answered 14/5, 2012 at 20:10 Comment(0)
S
31

See: Timestamp.Timestamp(long):

new Timestamp(someLong)
Shaper answered 14/5, 2012 at 20:10 Comment(0)
D
1

Yes, there's a constructor for Timestamp that takes a long as a parameter. http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#Timestamp(long)

Darindaring answered 14/5, 2012 at 20:17 Comment(0)
M
0

Java 8 Timestamp DOES include a constructor with long. documentation

Munn answered 28/1, 2023 at 1:10 Comment(0)
A
0

Use like below example:

Long expirationTime = 1719316525711;
Timestamp timestamp = new Timestamp(expirationTime);
Adductor answered 25/6 at 13:59 Comment(1)
format code using three backticksRemittent
Y
-1

This question is a bit old but for those arriving here scratching their heads because their Timestamp object does not include a (long) constructor, Firebase's com.google.firebase.Timestamp object include (Date date) constructor. and (long seconds, int nanoseconds) and Date does include a (long) constructor (which creates a date object based of number of seconds from epoch - same as java.sql.Timestamp

bottom line is your solution is simply either

Timestamp(new Date(longEpochTimeVar));

or

Timestamp(longEpochTimeVar,0);

0 stands for 0 nanoseconds so both option will produce the same result.

Yeld answered 10/2, 2020 at 11:58 Comment(1)
That's not working. Timestamp(new Date(longEpochTimeVar));Luettaluevano

© 2022 - 2024 — McMap. All rights reserved.