Search code examples
javatimespanpretty-print

Java pretty print for duration


Is there Java class or some sample code that can convert a java Date or Timestamp into something like:

"3 hours"
" 20 seconds"
 "25 minutes"

I need those strings in my web application to show how much it took to generate a file (in a pretty print way of course :) )

Thanks,


Solution

  • With JodaTime:

    PeriodFormat.getDefault( ).print( Hours.THREE );
    PeriodFormat.getDefault( ).print( Seconds.seconds( 25 ) );
    PeriodFormat.getDefault( ).print( Minutes.minutes( 20 ) );
    

    P.S. Also it's very easy to get the number of hours/seconds/minutes between 2 time points.