1

I want to convert the string time to Timestamp Object

My code for parsing is like this

String ts = "120918 10:35:45";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd hh:mm:ss");          
java.util.Date parsedDate = dateFormat.parse(ts);           
//parsing timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("timestamp after parsing :: "+timestamp);

It gives me result :-- timestamp after parsing :: 2012-09-18 10:35:45.0

But I do not want milliseconds part. I want only this -- 2012-09-18 10:35:45

Please help me in removing milliseconds part.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Anjali
  • 1,623
  • 5
  • 30
  • 50

3 Answers3

3

Timestamp is a container of milliseconds. The toString() is formatting it's contains based on what it thinks is best to be displayed.

If you want to format the value, you should use a date formatter and not use the value returned by the Timestamp object.

SimpleDateFormat noMilliSecondsFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(noMilliSecondsFormatter.format(timestamp));

nb. The value you have (after you've converted it) does not contain any milliseconds anyway...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i tried your code but it returns output same as input(120918 10:35:45). I want output in Timestamp object only. like this 2012-09-18 10:35:45 – Anjali Sep 26 '12 at 04:59
  • 1
    Really, I get `2012-09-18 10:35:45` which is apparently what you asked for. `Timestamp` is just a container, which contains the number of milliseconds from January 1, 1970, 00:00:00 GMT. It has no concept of formatting (be grateful it displays what it does, `1347928545000` would be even more useless). The best you can do use put the value through a formatter to get the format you want, that's its power. You can check out this previous discussion on the same subject http://stackoverflow.com/questions/12575990/calendar-date-to-yyyy-mm-dd-format-in-java/12576219#12576219 – MadProgrammer Sep 26 '12 at 05:06
  • 1
    @Anjali You need to create a new instance of `SimpleDateFormat`, say `newSDF` with new format "yyyy-MM-dd HH:mm:ss" and use it to format the `timestamp`. You must have used the `dateFormat` to format the `timestamp` and that will give you output same as input. – Gaurav Sep 26 '12 at 05:13
  • @TejasArjun, but it will give String but i want timestamp object. – Anjali Sep 26 '12 at 06:04
  • @Anjali The `Timestamp` object is correct. There's nothing wrong with. It is a valid representation of the value you parsed. The problem you're having is a formatting issue. You need to get over this idea that the trailing milliseconds (which is 0) is a problem. You have NO WAY to effect this, other then to use a formatter. – MadProgrammer Sep 26 '12 at 06:10
1

My guess, probably you didn't use "noMilliSecondsFormatter" in println().

Michael Chen
  • 5,438
  • 3
  • 15
  • 9
0

The method java.util.Date.getTime() according to its javaDoc:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object

Therefore, when you create the TimeStamp object, you are already passing mileseconds.

David Bejar
  • 512
  • 4
  • 20