tags:

views:

2183

answers:

4

I'd like to format a duration in seconds using a pattern like H:MM:SS. The current utilities in java are designed to format a time but not a duration.

+7  A: 

Use Joda Time and PeriodFormatter.

Joda Time is a much better date/time API than those in Java anyway :)

Jon Skeet
+8  A: 

If you don't want to drag in libraries, it's simple enough to do yourself using a Formatter, or related shortcut eg. given integer number of seconds s:

String.format("%d:%02d:%02d", s/3600, (s%3600)/60, (s%60))
bobince
Doesn't have to be integer. If you have two dates, just plug date1.getTime() - date2.getTime() into the above equation which uses the long primitive and still works.
Josh
A: 

This is going to be one of the new features in java 7

JSR-310

luke
A: 

Thanks bobince!! Easy and very good answer!!

jy