Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: How to display in HH:MM:SS... ? Date: Sun, 13 Oct 2013 09:57:53 +0200 Organization: A noiseless patient Spider Lines: 33 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 13 Oct 2013 07:57:53 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="b771b069f8002793a1813bddbe39641a"; logging-data="3973"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Rn6L/MaQEuQfS95OUNwEB" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0.1 In-Reply-To: Cancel-Lock: sha1:3pvjhBxZKgXQvzyI0Bd6FfKVhao= Xref: csiph.com comp.lang.java.programmer:26319 On 2013-10-10 19:58, abhilash.androiddeveloper@gmail.com allegedly wrote: > Hi, > > I am having two dates with times for example: > > String dateStart = "01/14/2012 09:30:55"; > String dateStop = "01/15/2012 18:25:54"; > > SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); > > Date d1 = null; > Date d2 = null; > > d1 = format.parse(dateStart); > d2 = format.parse(dateStop); > > When I subtract: d2 - d1, I want to show the ela[sed time in between the above two dates in the format in HH:MM:SS: like i want the output something like: > 33:05:01. How can I get in that format..? If anybody is having any idea please help me out... > public static String computeDiffTimeAsString( Date d1, Date d2 ){ if( d1.after( d2 ) ) throw new IllegalArgumentException( String.format( "Start date %tc is after end date %tc", d1, d2 ) ); long secs = (d2.getTime() - d1.getTime()) / 1000; return String.format( "%2d:%2d:%2d", secs / 3600, secs % 3600 / 60, secs % 60 ); } (Caveat: neither compiled nor tested) -- DF.