Re: How can I use Operand (+ , - ) with Time
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