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!!
Help in calculating time different!
Page 1 of 17 Replies - 1175 Views - Last Post: 11 October 2010 - 08:51 AM
Replies To: Help in calculating time different!
#2
Re: Help in calculating time different!
Posted 11 October 2010 - 05:44 AM
This site has a policy against just handing people the code they need. Show us what you've tried so far and we'll help
#3
Re: Help in calculating time different!
Posted 11 October 2010 - 06:08 AM
sure!! this is my coding!
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 !
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>");
}
}
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 post has been edited by ROCKY86: 11 October 2010 - 07:14 AM
#4
Re: Help in calculating time different!
Posted 11 October 2010 - 06:16 AM
Since you are using military time you should check and see if end time is less than start time and if it is then add 2400 (to simulate the next day) and then subtract it
#5
Re: Help in calculating time different!
Posted 11 October 2010 - 06:32 AM
Agentsnakezero, on 11 October 2010 - 05:16 AM, said:
Since you are using military time you should check and see if end time is less than start time and if it is then add 2400 (to simulate the next day) and then subtract it
I did that!!, if my end time is less then starting time, I add to 2359 then subtract the start time! however the condition is first two digits fall between 00 to 23 hour while its last two digits fall betwwen 00 to 59min!!
#6
Re: Help in calculating time different!
Posted 11 October 2010 - 07:18 AM
thats why you add 2400(thats the value of one day not 2359), and actually it doesnt matter what you have after you add the number because it subtractive and you always will get a normalized minuted or another way to test for everything is (time%100<=59) gives you minutes that are correct and (time/100<=24) will check hours. and physically if you wanted to using this
and then if your second time after the
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));
if(min2<min1){
min2+1440;
total=min2-min1;}
else
total=min2-min1;
that should workand then if your second time after the
This post has been edited by Agentsnakezero: 11 October 2010 - 07:22 AM
#7
Re: Help in calculating time different!
Posted 11 October 2010 - 07:41 AM
Agentsnakezero, on 11 October 2010 - 06:18 AM, said:
thats why you add 2400(thats the value of one day not 2359), and actually it doesnt matter what you have after you add the number because it subtractive and you always will get a normalized minuted or another way to test for everything is (time%100<=59) gives you minutes that are correct and (time/100<=24) will check hours. and physically if you wanted to using this
and then if your second time after the
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));
if(min2<min1){
min2+1440;
total=min2-min1;}
else
total=min2-min1;
that should workand then if your second time after the
thx for help! but the code look messy!! and hard to understand expecially this 2 statement:
int min1=(start%100+(start/100*60));
int min2=(end%100+(end/100*60));
is there any simply code to do it??
#8
Re: Help in calculating time different!
Posted 11 October 2010 - 08:51 AM
It looks fine to me. You can break up the math into multiple statements if you want, but I see nothing wrong with what you have.
What it does is take the remainder of start/100, and adds it to (start/100 * 60).
What it does is take the remainder of start/100, and adds it to (start/100 * 60).
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|