Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string date to Timestamp in java?

Tags:

java

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.

like image 655
lakshmi Avatar asked Apr 30 '10 05:04

lakshmi


People also ask

Can we convert String to timestamp in java?

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.

Can we convert String to 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.

Can we convert String to date in java?

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.


1 Answers

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.

like image 64
Glen Robertson Avatar answered Sep 20 '22 20:09

Glen Robertson