Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

Calculating a Time Difference

Thread view: 
wfs - 12 Feb 2006 16:54 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.