Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: How can I use Operand (+ , - ) with Time Date: Sun, 15 Jan 2012 10:50:08 -0800 Organization: albasani.net Lines: 92 Message-ID: References: <34afdd1f-4549-48a9-878c-8678993e97ed@t30g2000vbx.googlegroups.com> <4887cdd4-6a2e-49ac-9621-b594634fbf97@3g2000pbg.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net RbnsMrWVnSq6jCJu7MZtPrHGdTKcmE59S51dpX+xTkcXK61+vT9omVs27NsajZq3IgXgxiZGAhqVjzuqbL1bUjA0jFUHYyD0bWPMbJohXNBsBvDGKQ3dlpbn4ZTO6eVL NNTP-Posting-Date: Sun, 15 Jan 2012 18:50:10 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="tu1RPW6l/jg+Z/UzRw0DkACDjFGgKwVyEzkpae+Jx672DRe0cmQdzB0EDU9onopNYjAMqI2Ir3uz46ZV6iQy9CjpTRYKuvoEOPBHvyOpIlRHZy51WzlCwzYH7OiANgfL"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 In-Reply-To: <4887cdd4-6a2e-49ac-9621-b594634fbf97@3g2000pbg.googlegroups.com> Cancel-Lock: sha1:jMMzH6eIgudLLxFlkUHGhuX2PfY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11357 sahm wrote: > Thank you every one > > I fix the problem with simple function It is neither simple nor correct. It doesn't handle most date formats, it doesn't handle time zones, it doesn't handle Daylight Saving, it doesn't handle shifts that cross a midnight boundary. Weirdly, it doesn't use *any* of the standard date or time types, which _would_ have been simple and _could_ have been correct. Your variable names violate the Java coding conventions. So does your brace indentation. You show 'Time' as a type, but not its package. This will confuse anyone who thinks you mean the standard 'Time' class. I would never let this code past a code review. There is very little right about it. > \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ > Time getTotalOverTime(Time StrtTime, Time EndTime) Why isn't this method 'public'? > { > Time TotalHours = null; Why do you initialize this variable to 'null'? The value is never used. Also, you should declare variables close to the point of use, not all at the top, and could you have invented more obscure, hard-to-interpret variable names? I think not, save you used obfuscatory underscores throughout the names. > String ST, ET, TH; > int stH, stM, stS, etH, etM, etS, ttH, ttM, ttS; > > ST = String.valueOf(StrtTime); > ET = String.valueOf(EndTime); > > stH = Integer.parseInt(ST.substring(0, 2)); > stM = Integer.parseInt(ST.substring(3, 5)); > stS = Integer.parseInt(ST.substring(6, 8)); > > etH = Integer.parseInt(ET.substring(0, 2)); > etM = Integer.parseInt(ET.substring(3, 5)); > etS = Integer.parseInt(ET.substring(6, 8)); > > ttS = etS - stS; > if(ttS< 0) > { > ttS =+ 60; > etM =- 1; > } > > ttM = etM - stM; > if(ttM< 0) > { > ttM =+ 60; > etH =- 1; > } > > ttH = etH - stH; > > TH = String.valueOf(ttH) +":"+ String.valueOf(ttM) +":"+ > String.valueOf(ttS); > > TotalHours = Time.valueOf(TH); > > return TotalHours; > } Try again, using 'java.util.Calendar' and 'java.text.DateFormat' and their kin. The code that does the interval calculation should not use 'String' or in any part of the interval calculation; that's too many purposes for one routine. There should be absofrickinlutely no parsing left to do by the time you calculate intervals. Try making the method signature (for pre-Java 7 code, without the Joda library): /** * Calculates the interval in hours between two times. * * @param start Calendar start time of interval * @param finish Calendar finish time of interval * @return double the interval between the times in hours */ public double interval(Calendar start, Calendar finish); -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg