tags:

views:

260

answers:

4

This is probably a simple question but I can't seem to find the solution.

I have a time string that is 8 digits long (epoch seconds), when I try to format this using the Java DateFormat, it always assumes that my time contains milliseconds as well, so 16315118 converts to: 4:31:55.118 instead of the correct time of 19:58:38.

I do not want to edit the string to add in the milliseconds, so how can I do this?

I also do not want to multiply by 1000 since I am using this for formatting of other times that includes milliseconds.

+1  A: 

You can find the answer from Javas API here

Filip Ekberg
this does not help me, SimpleDateFormat still assumes the value I pass in to be parsed is in miliseconds instead of seconds.
yx
lol@RTFM, filip++
+1  A: 

multiply by 1000 and then chop off the last 4 characters of the string?

Jason S
+2  A: 

A Java Date is milliseconds since the epoch. Multiply your value by 1000 before you convert it to a Date. Then you can customize the DateFormat you use by creating a new SimpleDateFormat with the format string you want.

Paul Tomblin
+2  A: 

I also do not want to multiply by 1000 since I am using this for formatting of other times that includes milliseconds.

You're out of luck. You can't use the same DateFormat to format two different time values. Either use two different formatters or (more correctly) convert your time values.

Your time values should be in milliseconds because that is what the API expects. Anything else is a hack

Kevin
I see, that's too bad. Thanks.
yx