# JAVA 时分秒累加

```    @Test
public void TestA() throws ParseException {

SimpleDateFormat myFormatter = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//上一次通话时长
HealthConsultation healthConsultation = new HealthConsultation();
healthConsultation.setConsultinghours("00:00:08");

//呼叫时长 3
HealthCallStateRecord healthCallStateRecorda = new HealthCallStateRecord();
healthCallStateRecorda.setCreatedate(format.parse("2017-12-19 17:43:42"));

//本次通话时长 4
HealthCallStateRecord healthCallStateRecordb = new HealthCallStateRecord();
healthCallStateRecordb.setCreatedate(format.parse("2017-12-19 18:51:50"));

//4-3的时间 获得两个时间的毫秒时间差异
long diff = healthCallStateRecordb.getCreatedate().getTime() - healthCallStateRecorda.getCreatedate().getTime();//这样得到的差值是微秒级别
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
long sec = diff % nd % nh % nm / ns;

//判断上次是否有通话，如果有通话时长，就都累加
if(healthConsultation.getConsultinghours() != null){
Calendar cal = Calendar.getInstance();
cal.setTime(myFormatter.parse(healthConsultation.getConsultinghours()));
int shi = (int)cal.get(Calendar.HOUR_OF_DAY)+(int)hour;
int fendo = (int)cal.get(Calendar.MINUTE)+(int)min;
int miao = (int)cal.get(Calendar.SECOND)+(int)sec;

//秒大于60设为00,然后判断分是否大于60分钟,如果是则分钟清0,小时加1,否则，分钟加1。
if(miao>60){
miao=00;
if(fendo>60){
fendo = 00;
shi = shi+1;
}else {
fendo = fendo + 1;
}
}
Date dates = myFormatter.parse(shi + ":" + fendo + ":" + miao);
System.out.println("不为空当前时间 : " + myFormatter.format(dates));
}else {
//如果上次无通话，就把本次时长，设为通话时长。
Date dates = myFormatter.parse(hour + ":" + min + ":" + sec);
System.out.println("为空当前时间 : " + myFormatter.format(dates));
}

}```

`不为空当前时间 : 01:08:16`