# Help in calculating time different!

• 10-11-2010, 01:37 PM
rocky86
Help in calculating time different!
hi guys! i need help on calculating about the time differences in 24hour format!! A program that reads in start time, end time then output the time difference!!

example: Enter Start time: 2355
Enter End time : 0005
Time difference is 10min

I have no idea how to do that!! I understand I can take end time to minus the start time, however that don't give me the time differences!!
• 10-11-2010, 02:17 PM
KevinWorkman
How would you do this on paper? Write out the steps you do in your head to calculate the answer. Then start translating those steps to code.

You might want to take a look at the Calendar class. But chances are, this is for an assignment, and you're supposed to come up with your own algorithm.
• 10-11-2010, 03:21 PM
rocky86
basically what my program do is ask for user input! after that it print out the time differences between start and end! if the user enter -1 it will terminate the program! however! if user enter

Start time:2311
end Time: 2321
Time difference is 10min

This output is correct! BUT

Start time:0021
end Time: 0011
Time difference is 2350min

that wrong!THE time difference should be 1430min but I got no idea how to calculate to make it !

this is my code:

Code:

```int start=0, end=0, hour=0, diffmin=0,min=2360; System.out.print("Enter starting time: "); start = sc.nextInt();     while(start!=-1) { System.out.print("Enter ending time: "); end = sc.nextInt(); if(end>start) { diffmin = end - start;     } else if(end<start) { end+=min; diffmin=end-start; } System.out.println("Time difference is "+diffmin+ " min"); start=0;//reset to 0 end=0;//reset to 0 System.out.print("Enter starting time: "); start = sc.nextInt(); } System.out.print("<Program terminates>"); } }```
• 10-11-2010, 03:33 PM
KevinWorkman
Quote:

Originally Posted by rocky86
basically what my program do is ask for user input! after that it print out the time differences between start and end! if the user enter -1 it will terminate the program!

Okay!

Quote:

Originally Posted by rocky86
Start time:0021
end Time: 0011
Time difference is 2350min

that wrong!

Why? If it is 12:21 AM, the next time it will be 12:11 AM is in 23 hours and 50 minutes.

Quote:

Originally Posted by rocky86
THE time difference should be 1430min but I got no idea how to calculate to make it !

Hm, I see. Are you sure you fully understand the requirements? It seems strange to do it that way. Assuming you do have the requirements down, again, think about how you do this in your head: if something is 23 hours and 50 minutes in the future, how did you figure out that it is 1,430 minutes away?
• 10-11-2010, 03:35 PM
rocky86
erm! the 1430min is a example given by my assignment!
• 10-11-2010, 03:50 PM
KevinWorkman
I guess I'm confused about the requirements. Is the time input in HH:MM (or HHMM) format? Or is it just a basic count of minutes? Does 0100 represent 1:00 AM, or is it 100 minutes into the interval? You seem to be switching back and forth between the two formats.
• 10-11-2010, 04:18 PM
JosAH
If Tend < Tstart we can assume that Tend is in the next day; so there are 24 hours - Tstart+Tend minutes between the two times, i.e. 1440 minutes - Tstart +Tend (all measured in minutes relative to midnight)

kind regards,

Jos