简体   繁体   English

如何计算三个时间之间的差异?

[英]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. 它不必是Date对象。

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. 因此,您将有2个Date对象(一个用于中断时间,另一个用于中断时间结束),而不只是一个用于中断时间。

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) 因为如果您执行date1- date2- date3,则您会有一个负数(es 1535983083-1535810283-1535896683)

The key problem here is that date3 , the duration of the break time, is using an unsuitable object. 这里的关键问题是date3 (休息时间的持续时间)正在使用不合适的对象。 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 ). date1date2被正确定义为Date对象,因为它们代表一天中的时间(如您在评论中提到的10:00和05:00)。 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. 但是,通过将date3设置为00:30,您将其声明为等于今天午夜半的一天中的某个时间,而您希望将其设置为30分钟。

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): 因此,您需要采用date1date2之间的差值,然后像这样从休息时间中减去分钟数(例如,假设breakLength为30分钟):

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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