I’m trying to convert an amount of seconds into a formatted string using midp
Below is code I’m using, the problem is that when I run this on a device (BlackBerry), 2 hours are being appended. So –
formatSecondsAsDuration(1000) returns “00:02:01”
What I expect is “00:00:01”
I think this is occuring since SimpleDateFormat is using my locale ?
I am unable to use the method setTimeZone to set the timezone to UTC which I think could fix the issue.
public static String formatSecondsAsDuration(long second) {
Date date = new Date(second);
return new SimpleDateFormat("HH:mm:ss").format(date);
}
I have already asked a similar question – Convert int val to format "HH:MM:SS" using JDK1.4 But have since found was not fully suitable
What about something really simple like:
Just make sure you actually pass seconds (as indicated by the parameter name), or else modify the code above to handle milliseconds if that’s what you’re actually needing.
EDIT:
I just ran across this method today that looks like it does exactly what you want and is already built-in to the BB API: DateTimeUtilities.formatElapsedTime()