我有四个日期,我想获得总减去...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 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()));
使用SimpleDateFormat格式化您的总时间 – zgc7009
但它给了我错误的计算结果应该是8:00而不是16:00 – askManiac
您使用的是什么版本的Java? –