<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "A simple duration method"]]></title>
		<link>http://forums.hotjoe.com/posts/list/27.page</link>
		<description><![CDATA[Latest messages posted in the topic "A simple duration method"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>A simple duration method</title>
				<description><![CDATA[ I have often been frustrated that the Java standard library does not do a good job with time durations.  Sure, there are plenty of classes and methods to deal with time and date but nothing that is meant to use a when you have a delta between two timestamps.<br /> <br /> So I wrote a very simple method to simply print how long of a duration a number of milliseconds is.  It only goes to hours so if your duration is longer than that there will be many hours.  Additionally, this requires Java 5 and above.<br /> <br /> [code]<br /> import java.io.ByteArrayOutputStream;<br /> import java.io.PrintWriter;<br /> <br /> <br /> public class Duration<br /> {<br />     public static void main( String[] argv )<br />     {<br />         Duration duration = new Duration();<br /> <br />         System.out.println( "duration is " + duration.printDuration( 3601001, true ) );<br />     }<br /> <br />     private String printDuration( long milliseconds, boolean longFormat )<br />     {<br />         long hours = milliseconds / ((long)(1000 * 60 * 60));<br />         long minutes = (milliseconds % (1000 * 60 * 60)) / ((long)(1000 * 60 ));<br />         long seconds = (milliseconds % (1000 * 60 * 60) % (1000 * 60)) / (long)1000;<br />         long millisecondsRemainder = milliseconds % (long)1000;<br /> <br />         ByteArrayOutputStream baos = new ByteArrayOutputStream();<br /> <br />         PrintWriter writer = new PrintWriter( baos );<br /> <br />         if( longFormat )<br />              writer.printf( "%1$d hour%2$s %3$d minute%4$s %5$d second%6$s and %7$d millisecond%8$s",<br />                             hours, (hours == 1 ? "" : "s" ),<br />                             minutes, (minutes == 1 ? "" : "s"),<br />                             seconds, (seconds == 1 ? "" : "s"),<br />                             millisecondsRemainder, (millisecondsRemainder == 1 ? "" : "s" ) );<br />         else<br />             writer.printf( "%1$02d:%2$02d:%3$02d.%4$03d", hours, minutes, seconds, millisecondsRemainder );<br /> <br />         writer.close();<br /> <br />         return( baos.toString() );<br />     }<br /> }<br /> [/code]]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/493/1902.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/493/1902.page</link>
				<pubDate><![CDATA[Fri, 22 Aug 2008 10:02:51]]> GMT</pubDate>
				<author><![CDATA[ stdunbar]]></author>
			</item>
	</channel>
</rss>
