z  - z - - 28 days ago 21
Java Question

DateFormat with no miliseconds

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.

Answer

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