-2

I am trying to convert String to java Timestamp. I am using the following code

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy  HH:mm:ss:SSS");
Date parsedDate = dateFormat.parse(value);
Timestamp timestamp = new Timestamp(parsedDate.getTime());            
return timestamp;            

My input is Input ::"10/01/2012 06:45:23:245946"

But i am getting Wrong Output :: 2012-10-01 06:49:28.946

What is Wrong in this code??

Java_Dinesh
  • 145
  • 1
  • 3
  • 13
  • 2
    Geeez .. how many times do you plan to post the same question?! – Dirk Nov 28 '12 at 08:26
  • possible duplicate of [How do I convert the following String Date into Java Data format in Java?](http://stackoverflow.com/questions/13599865/how-do-i-convert-the-following-string-date-into-java-data-format-in-java) – Perception Nov 28 '12 at 08:29
  • @Java_Dinesh - your question was already answered [here](http://stackoverflow.com/a/13599926/680925). Your really large input values for milliseconds is being converted into minutes, you need to restrict it down to three digits. – Perception Nov 28 '12 at 08:30
  • You have the answer in your same previous question. – Nelson Nov 28 '12 at 08:31
  • Hi all i am really sorry for the unwanted post. But the value 245946 is actully a micro seconds.How do i handle micro seconds in SimpledateFormat. Note: 1.I need the exact value as output 2. I get the input using the following oracle Query (select to_char(systimestamp,'YYYY-MM-DD HH24:MI:SS.FF') from dual;). Thank you – Java_Dinesh Nov 28 '12 at 13:54
  • Hi any one ans me please – Java_Dinesh Nov 29 '12 at 07:01

3 Answers3

1
10/01/2012 06:45:23:245946

Here you have passed 245946 milliseconds, which is actually converted into seconds and minutes and it become 2012-10-01 06:49:28.946.

245946 milliseconds = 245 seconds and 946 milliseconds 
245 seconds 946 milliseconds = 4 minutes, 5 seconds and 946 milliseconds

so 06:45:23+ 4 minutes 5 seconds and 946 milliseconds = 
06 hour 49 minutes 28 seconds 946 miliseconds
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

It's correct, all those extra milliseconds are just represented as full minutes and seconds when you print it back out. Your input includes 245946 milliseconds, which is 4 minutes, 5 seconds, and 946 milliseconds. So your output shows as 4 minutes and 5 seconds later with 946 trailing millis.

Affe
  • 47,174
  • 11
  • 83
  • 83
  • Hi you are right. But 245946 is micro seconds. how do i mention the microseconds in SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SSS"); – Java_Dinesh Nov 28 '12 at 09:40
0

Step1: Parse given Date

Step2: Use the following constructor to get absolute timestamp

java.sql.Timestamp timestamp = new Timestamp((year-1900), (month-1), date, hour, min, sec,nano);
Java_Dinesh
  • 145
  • 1
  • 3
  • 13