时分秒 java_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通話時長為1個多小時。
總結
以上是生活随笔為你收集整理的时分秒 java_JAVA 时分秒累加的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kafka topic 一段时间不消费_
- 下一篇: c语言程序编写一朵花,一朵花(中英双语)