简体   繁体   中英

How to calculate difference between three time?

Works with two different times but not showing correct calculation with three different times.

SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date1 = format.parse(timestart);
Date date2 = format.parse(timeend);
Date date3 = format.parse(breaktime);
long mills = date2.getTime() - date1.getTime() - date3.getTime();
int hours = (int)(mills/(1000 * 60 * 60));
int mins = (int) mills/(1000*60) %60;


Are you trying to calculate:

Work_Duration = End_Time - Start_Time - Break_Duration?

If yes, you can simply use an integer/long variable to represent break duration in units of your choice (seconds or minutes). It need not be a Date object.

You can calculate:

Break_Duration = Break_End_Time - Break_Start_Time

So, you will have 2 Date objects (one for start of break, another for the end of break time), instead of just one for breaktime.

If you want calculate the different between timeend end timestart and the different between timeend end breaktime time you need do:

SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date1 = format.parse(timestart);
Date date2 = format.parse(timeend);
Date date3 = format.parse(breaktime);
long mills = date2.getTime() - date1.getTime();
long mills2 = date2.getTime() - date3.getTime();
int hours = (int)(mills/(1000 * 60 * 60));
int mins = (int) mills/(1000*60);
int hours2 = (int)(mills/(1000 * 60 * 60));
int mins2 = (int) mills/(1000*60);

Because if you do date1- date2- date3 you have a negative number (es 1535983083-1535810283-1535896683)

The key problem here is that date3 , the duration of the break time, is using an unsuitable object. date1 and date2 are correctly defined as Date objects as they represent times of the day (10:00 and 05:00, as you mention in your comment ). However, by setting date3 to be 00:30, you're declaring it to being a time of day equivalent to half past midnight today , instead you want this to be a time period of 30 minutes.

So you'll want to take the difference between date1 and date2 , and then subtract the number of minutes taken from the break like this (assuming breakLength is 30 minutes for example):

SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date1 = format.parse(timestart);
Date date2 = format.parse(timeend);
long breakLengthMillis = TimeUnit.MINUTES.toMillis(breakLength)

long mills = date2.getTime() - date1.getTime() - breakLengthMillis;
int hours = (int)(mills/(1000 * 60 * 60));
int mins = (int) mills/(1000*60) % 60;

Alternatively, if you want to measure a break by its start and end times, you could use this:

SimpleDateFormat format = new SimpleDateFormat("hh:mm");
Date date1 = format.parse(timestart);
Date date2 = format.parse(timeend);
Date date3 = format.parse(breakStart);       // Start time of break
Date date4 = format.parse(breakEnd);         // End time of break

long mills = date2.getTime() - date1.getTime() - (date4.getTime() - date3.getTime());
int hours = (int)(mills/(1000 * 60 * 60));
int mins = (int) mills/(1000*60) % 60;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM