I want to convert string Date into Timestamp in java. The following coding i have written.I have declare the date for date1 is: 7-11-11 12:13:14.
SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date lFromDate1 = datetimeFormatter1.parse(date1); System.out.println("gpsdate :" + lFromDate1); Timestamp fromTS1 = new Timestamp(lFromDate1.getTime());
I want to convert 7-11-11 12:13:14 this string date into timestamp. Now i got the output is 0007-11-11 00:13:14.000000 +05:30:00 but i want ( 7-11-11 12:13:14) this format Timestamp date. Can anyone please help me. Thank you.
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.
To convert a date string to a timestamp: Pass the date string to the Date() constructor. Call the getTime() method on the Date object. The getTime method returns the number of milliseconds since the Unix Epoch.
By default, Java dates are in the ISO-8601 format, so if we have any string which represents a date and time in this format, then we can use the parse() API of these classes directly.
All you need to do is change the string within the java.text.SimpleDateFormat
constructor to: "MM-dd-yyyy HH:mm:ss".
Just use the appropriate letters to build the above string to match your input date.
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