0

I have searched a lot for the solution of this problem.But nothing could suggest an appropriate solution.

I have date ' 03/02/2015 12:00:00 AM' in string format .how can i convert it to timestamp?

I have tried with the following code

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String str = formatter.format((Date) fromTimeInTimestamp);//fromTimeInTimestamp is 2015-03-02 00:00:00.0

but I need str in timestamp format.How can I get this?

Adhi
  • 35
  • 1
  • 7
  • 1
    possible duplicate of [Java: Convert String to TimeStamp](http://stackoverflow.com/questions/18915075/java-convert-string-to-timestamp) – Wai Ha Lee Mar 10 '15 at 04:08
  • What is *"timestamp format"*? – MadProgrammer Mar 10 '15 at 04:08
  • 1
    `2015-03-02 00:00:00.0` doesn't match `03/02/2015 12:00:00 AM`...? – MadProgrammer Mar 10 '15 at 04:09
  • As a result of above code snippet str will give '03/02/2015 12:00:00 AM' as output.But it is of String type and I need the same in timestamp type – Adhi Mar 10 '15 at 04:12
  • possible duplicate of [How to convert date to timestamp in PHP?](http://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php) – coDe murDerer Mar 10 '15 at 04:13
  • `TimeStamp` like `Date` doesn't have a format. They are a container for the number of milliseconds since the Unix epoch. If you are inserting the value into the database, then use the `TimeStamp` class directly and let the JDBC driver figure it out – MadProgrammer Mar 10 '15 at 04:15
  • @CoDe MurDeRer No.I am searching for the solution in java – Adhi Mar 10 '15 at 04:16
  • @Adhi To address an individual, use `@{name}` in you comments – MadProgrammer Mar 10 '15 at 04:17

1 Answers1

0
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String str = "";
try {
   str = formatter.format(formatter.parse(fromTimeInTimestamp));
} catch (ParseException e) {
   e.PrintStackTrace();
}

You can try something like this. It should work

N00dl3
  • 1
  • 1