Hi ,

I have two string which have time in them like

one= 11:54:30.360
two= 11:54:30,427

Wht is the best way to get the difference between them


Thanks

1/ are you sure that difference between Time will never be more that one day
2/ what value have result, miliseconds, seconds ....

java.util.date knows value as long (number in miliseconds)

please edit your post

two= 11:54:30.427

Edited 5 Years Ago by mKorbel: n/a

ya the difference is always at max differed by seconds................and the result can be anything milliseconds or seconds........and its a comma between seconds and milliseconds not a '.' ..... kindly suggest something

You can start by checking out the java.util.GregorianCalendar class. You'll need to study it a bit to understand how it operates with its own fields such as HOUR, MINUTE, SECOND, MILLISECOND, the compareTo() method. If the times are presented as strings, then you'll also need to look at String.split() to get the various elements of the time.

Hope this helps as a start.

I have two string which have time in them like

one= 11:54:30.360
two= 11:54:30,427

Wht is the best way to get the difference between them

IMO, the best way would be to convert both of those to proper Date objects using the SimpleDateFormat class and then apply normal Date diff algorithms/methods to the same.

// sample pseudocode; untested
def sdf = new SimpleDateFormat("HH:mm:ss.S")
def dt1 = sdf.parse("11:54:30.360")
def dt2 = sdf.parse("11:54:30,427")
// use dates dt1 and dt2
This article has been dead for over six months. Start a new discussion instead.