Java添加和减去时间

时间:2015-01-10 17:43:54

标签: java

我有四个约会,我想得到总数...

示例

timeInAM = 9:00

timeOutAM = 12:00

timeInPM = 13:00

timeOutPM = 18:00

我想让total=(timeOutAM-timeInAM)+(timeOutPM-timeInPM)导致总数= 8:00

但它给了我'16:00:00'

这是我做的: 的 DATE

SimpleDateFormat tf24=new SimpleDateFormat("HH:mm");
Date timeInAM=new Date();
Date timeOutAM=new Date();
Date timeInPM=new Date();
Date timeOutPM=new Date();
long total;

timeInAM=tf24.parse(tblWorkPeriod.getValueAt(i, 1).toString());
timeOutAM=tf24.parse(tblWorkPeriod.getValueAt(i, 2).toString());
timeInPM=tf24.parse(tblWorkPeriod.getValueAt(i, 3).toString());
timeOutPM=tf24.parse(tblWorkPeriod.getValueAt(i, 4).toString());
total=(timeOutAM.getTime()-timeInAM.getTime())+(timeOutPM.getTime()-timeInPM.getTime());
System.out.println(tf24.format(new Date(total)));

CALENDAR

Calendar timeInAM=Calendar.getInstance();
Calendar timeOutAM=Calendar.getInstance();
Calendar timeInPM=Calendar.getInstance();
Calendar timeOutPM=Calendar.getInstance();
Calendar total=Calendar.getInstance();

SimpleDateFormat tf24=new SimpleDateFormat("HH:mm");

timeInAM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 1).toString()));
timeOutAM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 2).toString()));
timeInPM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 3).toString()));
timeOutPM.setTime(tf24.parse(tblWorkPeriod.getValueAt(i, 4).toString()));
long sum=(timeOutAM.getTimeInMillis()-timeInAM.getTimeInMillis())+(timeOutPM.getTimeInMillis()-timeInPM.getTimeInMillis());
total.setTimeInMillis(sum);
System.out.println("total : "+tf24.format(total.getTime()));

3 个答案:

答案 0 :(得分:2)

您可以尝试JodaTime库(如果您可以使用其他库)。通过以下内容,您可以通过调用LocalTime::minusHours和类似命令来实现所需:

LocalTime timeInAM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeOutAM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeInPM=new LocalTime(hourOfDay, minuteOfHour);
LocalTime timeOutPM=new LocalTime(hourOfDay, minuteOfHour);

LocalTime amInterval = timeOutAM.minusHours(timeInAM.getHourOfDay()).minusMinutes(timeInAM.getMinuteOfHour());
LocalTime pmInterval = timeOutPM.minusHours(timeInPM.getHourOfDay()).minusMinutes(timeInPM.getMinuteOfHour());

LocalTime total = pmInterval.plusHours(amInterval.getHourOfDay()).plusMinutes(amInterval.getMinuteOfHour());

使用正确的 DateTimeFormatter 来解析/打印 LocalTime 中的日期。

答案 1 :(得分:0)

我不确定我是否理解你的问题,但是在这里:

您可以尝试使用:

double timeInAMint = Double.parseDouble(timeInAM);
double timeOutAMint = Double.parseDouble(timeoutAM);
double timeInPMint = Double.parseDouble(timeInPM);
double timeOutPMint = Double.parseDouble(timeOutPM);
double sum = timeInAMint + timeOutAMint + timeInPMint + timeOutPMint;
System.out.println("Sum: " + sum);

答案 2 :(得分:0)

尝试使用Calendar实例。

编辑:示例实施。

Calendar timeInBeforeBreak = Calendar.getInstance();
timeInBeforeBreak.clear();

Calendar timeOutBeforeBreak = Calendar.getInstance();
timeOutBeforeBreak.clear();

timeInBeforeBreak.add(Calendar.HOUR_OF_DAY, 9);
timeInBeforeBreak.add(Calendar.MINUTE, 30);
timeOutBeforeBreak.add(Calendar.HOUR_OF_DAY, 11);
timeOutBeforeBreak.add(Calendar.MINUTE, 30);

long timeMillis = timeOutBeforeBreak.getTimeInMillis() - timeInBeforeBreak.getTimeInMillis();

System.out.println("Hours :"+timeMillis/1000/60/60);