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
?
Use TimeStamp. valueOf() to Convert a String to Timestamp in Java. We will use the TimeStamp class's own static function - valueOf() . It takes a string as an argument and then converts it to a timestamp.
We can convert long to int in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype).
Java Program to Convert Date to TimeStamp 1 The constructor of the time-stamp class requires a long value. 2 So data needs to be converted into a long value by using the getTime () method of the date class (which is present in... More ...
So, you can directly assign an instance of the Timestamp class to Date. In such a case, the output of the Date object will be like Timestamp ie it would be in the format like date followed by time (to the scale of milliseconds).
String timeInLong = time.replaceAll (":", ""); System.out.println ("Time in long format : "+Long.parseLong (timeInLong)); Show activity on this post. Thanks for contributing an answer to Stack Overflow!
No need for java.sql.Timestamp. The modern approach uses the java.time classes that supplant the troublesome old legacy classes such as java.sql.Timestamp & Date & Calendar.
The constructor is doing that:
Timestamp(long time)
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