Link to home
Start Free TrialLog in
Avatar of OliviaRedhorse
OliviaRedhorseFlag for United States of America

asked on

How to calcuate time duration from 2 time strings?

Hello All,
I am having writers block.  I would like to calculate the time duration from 2 strings that are input by the user.  I know I need to convert the strings to date/time format or integer.  The time strings would look something like this; start time - 10:35:22.189 and end time - 11:41:12.120.  Also, notice I need milliseconds.  Because I am working with just time, I am having trouble determining which calendar/date function to use because they involve using the date as well.  Instead of date, the user is entering the Day of Year, so it is any integer that is within the 1 to 365 days. So, the timestamp format would be 219/10:35:22.189.  What is the best method to use for this calculation? Any resources, recommendations or examples would be very helpful.  Thank you.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
e.g.
public static long timeDiff(String t1, String t2) throws ParseException {
	//10:35:22.189 and end time - 11:41:12.120.
	DateFormat df = new SimpleDateFormat("HH:mm:ss.S");
	Date start = df.parse(t1);
	Date end =  df.parse(t2);
	return TimeDiff.getTimeDifference(start, end, TimeDiff.TimeField.MILLISECOND);
    }

Open in new window

Oh sorry - if you want all milliseconds, it's better to use for_yan's method, which can be simplified:


public static long timeDiff(String t1, String t2) throws ParseException {
	DateFormat df = new SimpleDateFormat("HH:mm:ss.S");
	return df.parse(t2).getTime() - df.parse(t1).getTime();
    }

Open in new window

With clearer naming:
public static long timeDiff(String startTime, String endTime) throws ParseException {
	DateFormat df = new SimpleDateFormat("HH:mm:ss.S");
	return df.parse(endTime).getTime() - df.parse(startTime).getTime();
    }

Open in new window

Avatar of OliviaRedhorse

ASKER

Thank you for your quick responses.

In using the end.getTime() - start.getTime() formula, this turns the time difference in milliseconds, correct?  If I need to display the time format HH:mm:ss.SSS, I would need to add logic that converts the milliseconds to hour, minutes and seconds.
One more question.  Should I be concerned that when I am only working with time, the date is defaulted to 1/1/1970?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> If I need to display the time format HH:mm:ss.SSS, I would need to add logic that converts the milliseconds to hour, minutes and seconds.

You could use the code that i originally gave you
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can actually just use the same DateFormat and do
String hms_ms = df.format(new Date(diff));

Open in new window



You would think so, but in fact at least SimpleDateFormat (don't now about
when you format

 SimpleDateFormat f = new SimpleDateFormat("hh:mm:ss.SSS");
        Date d1 = f.parse(s1,new ParsePosition(0));
        Date d2 = f.parse(s2,new ParsePosition(0));

        long msec = d2.getTime() - d1.getTime();

        System.out.println("msec:" + msec);

        Date d3 = new Date(msec );
        System.out.println("date: " + d3);
        String ss = f.format(d3);
         System.out.println("ss:" + ss);
returns
17:05:49.931

instead of 01:05:49.931

even though msec returns correctly:

msec: 3949931
(a little bit more than an hour ofr those two points you specified).

Don't understand how it can be







You would think so, but in fact it does not work this way:
at least  with SimpleDateFormat
when you format it like that:

 SimpleDateFormat f = new SimpleDateFormat("hh:mm:ss.SSS");
        Date d1 = f.parse(s1,new ParsePosition(0));
        Date d2 = f.parse(s2,new ParsePosition(0));

        long msec = d2.getTime() - d1.getTime();

        System.out.println("msec:" + msec);

        Date d3 = new Date(msec );
        System.out.println("date: " + d3);
        String ss = f.format(d3);
         System.out.println("ss:" + ss);
returns
17:05:49.931

instead of 01:05:49.931

even though msec returns correctly:

msec: 3949931
(a little bit more than an hour ofr those two points you specified).

Don't understand how it can be




If you read the link I posted above it explains that you can only use DateFormat if the period is less than 24 hours.
Sorry yes - on the DateFormat, you'd need to do
df.setTimeZone(TimeZone.getTimeZone("UTC"));

Open in new window


BUt DateFormat gives the same result.
I guess it is about Time Zone or somthing it is because Im in pAcificTome

     String s1 = "10:35:22.189";
        String s2 = "11:41:12.120";

        DateFormat f = new SimpleDateFormat("HH:mm:ss.SSS", Locale.UK);
        Date d1 = f.parse(s1,new ParsePosition(0));
        Date d2 = f.parse(s2,new ParsePosition(0));

        long msec = d2.getTime() - d1.getTime();

        System.out.println("msec:" + msec);

        Date d3 = new Date(msec );
        System.out.println("date: " + d3);
        String ss = f.format(d3);
         System.out.println("ss:" + ss);

...so
f.setTimeZone(TimeZone.getTimeZone("UTC"));
String ss = f.format(d3);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial