tags:

views:

244

answers:

5

Hi, I have two times in an array and I need to calculate the difference between both of these. I have converted the hours into mins, then added the remaining mins. This gives me the total minuites overall, once I did this for both I simply minus one total mins from the other. Then converted them back to hours and minuites.

  double no1 = Double.parseDouble(array[i][4]);

  int time1_calc = (int) (no1 * 100); //Remove decimal point

  int time1hours = (time1_calc) / 100;

  int time1mins = (time1_calc) % 100;

  int time1HM = time1hours*60;

  int time1_total = time1HM + time1mins;

The above code is used for the second time, I then use:

 int total = time2_total - time1_total;

For this all the calculations "look" to work, but for example the difference betweeen 10.18 and 09.35 is 1hour 23mins or 83mins in total. My program seems to show 43mins.

I have tried other ways but still cannot get it working, any ideas?

Thanks =)

+2  A: 

Have a look at the Joda library.

If you want to do it by yourself, then your code looks correct to me.

kgiannakakis
Thanks but I cannot use any libarys.
Elliott
@Elliott: Why not, out of interest? Joda time is definitely the way to go if you possibly can.
Jon Skeet
Its for an assignment, so it has to be all written in code.
Elliott
+2  A: 
int time1mins = (time1_calc) % 100;

Minutes are not the remainder of hours divided by 100, but by 60. Is that what that bit of code is meant to be doing?

Also, you might be better off just using a Calendar object.

Robert Grant
I fear the OP has a String like "10.18" representing 10:18 and uses parseDouble to convert it to a double. In that case using "% 100" to get the minutes would be "correct". But the entire approach is broken.
Joachim Sauer
I am using the mod function to get the remainder minuites. This just separtes the hours from the mins.
Elliott
@Elliott: that approach is fundamentally broken. Just think what happens when the String is "10.2" or "10.02". Should that result in the same time being used? Does it result in the same time being used?
Joachim Sauer
Sorry, I dont understand what your asking. When I try another time I get something totally wrong, what would you recommend to do the calculation? Thanks.
Elliott
+3  A: 

Use a DateFormat (probably SimpleDateFormat) object to parse your String and don't try to use parseDouble() (that's for parsing a single number, which a time is not).

Joachim Sauer
Using DateFormat you can get the time difference between two date objects
prakash.panjwani
A: 

Use the Date Object. Then Use its getTime method. It will give you the time in milliseconds(long). Then subtract the times. and then convert them to minutes and hours.

dev ray
+1  A: 

Calendar time1 = Calendar.getInstance(); // assign time1 date from your array

Calendar time2 = Calendar.getInstance(); // assign time2 date from your array

long diff = time1.getTimeInMills() - time2.getTimeInMills();

srinannapa
do I need to import the calendar function?
Elliott
Calendar is a class
srinannapa