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;
3 个解决方案
#1
0
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.
这里的关键问题是date3(中断时间的持续时间)使用的是不合适的对象。 date1和date2被正确定义为Date对象,因为它们代表当天的时间(10:00和05:00,正如您在评论中提到的那样)。但是,通过将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):
所以你想要取date1和date2之间的差值,然后减去断点所取的分钟数(假设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;
#2
3
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对象(一个用于开始休息,另一个用于休息时间结束),而不是一个用于休息时间。
#3
0
If you want calculate the different between timeend end timestart and the different between timeend end breaktime time you need do:
如果你想要计算timeend end timestart和timeend end breaktime time之间的差异你需要做的事情:
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)
#1
0
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.
这里的关键问题是date3(中断时间的持续时间)使用的是不合适的对象。 date1和date2被正确定义为Date对象,因为它们代表当天的时间(10:00和05:00,正如您在评论中提到的那样)。但是,通过将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):
所以你想要取date1和date2之间的差值,然后减去断点所取的分钟数(假设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;
#2
3
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对象(一个用于开始休息,另一个用于休息时间结束),而不是一个用于休息时间。
#3
0
If you want calculate the different between timeend end timestart and the different between timeend end breaktime time you need do:
如果你想要计算timeend end timestart和timeend end breaktime time之间的差异你需要做的事情:
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)