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

Recommended Answers

All 4 Replies

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.