Hi All,
any idea how to calculate the time difference between 2 'strings'
so for '22:00:00' and '01:00:00'
I'd like the result to be 180 minutes....
TIA
Bill
- I tried the following - but the results don't make sense
Time time10pm, time01am;
long t1, t2, timeDiff;
time10pm = Time.valueOf("22:00:00");
t1 = time10pm.getTime(); // t1 = 97,200,000
time01am = Time.valueOf("01:00:00");
t2 = time01am.getTime(); // t2 = 21,600,000
Ian Mills - 12 Feb 2006 17:28 GMT
> Hi All,
>
[quoted text clipped - 19 lines]
> time01am = Time.valueOf("01:00:00");
> t2 = time01am.getTime(); // t2 = 21,600,000
What do you mean the results don't make sense? (I would suggest that
possibly you need to read the Javadoc for getTime() method in Date)
Stefan Ram - 13 Feb 2006 09:06 GMT
> any idea how to calculate the time difference between 2 'strings'
> so for '22:00:00' and '01:00:00'
> I'd like the result to be 180 minutes....
class Hms
{ public int h; public int m; public int s;
public Hms( final java.lang.String hms )
{ this.h = java.lang.Integer.valueOf
( hms.replaceAll( "(\\d\\d):\\d\\d:\\d\\d", "$1" ));
this.m = java.lang.Integer.valueOf
( hms.replaceAll( "\\d\\d:(\\d\\d):\\d\\d", "$1" ));
this.s = java.lang.Integer.valueOf
( hms.replaceAll( "\\d\\d:\\d\\d:(\\d\\d)", "$1" )); }
public int minus( final Hms other )
{ final int diff=( this.h - other.h )* 60 +( this.m - other.m );
return diff < 0 ? 24 * 60 + diff : diff; }}
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( new Hms( "01:00:00" ).minus( new Hms( "22:00:00" ))); }}
JR - 14 Feb 2006 17:01 GMT
if all you want is minute differentials; why not multiple each value
by 60 and do some modular math?
Roedy Green - 14 Feb 2006 17:26 GMT
> if all you want is minute differentials; why not multiple each value
>by 60 and do some modular math?
see http://mindprod.com/jgloss/modulus.html#MIXEDBASE

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.