我没有时间。
例如: -
01:00:06
02:30:00
05:00:09
01:59:06
10:15:06
我想要添加所有这些时间。最后计算小时,分钟和秒钟的方式。
请让我一个想法来解决这个问题。
我想尝试使用Calendar
。
更新: -
private void test() {
String[] dates = { "01:00:06", "02:30:00", "05:00:09", "01:59:06",
"10:15:06" };
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
long totalHours;
for (int i = 0; i < dates.length; i++) {
calendar.setTime(ConstantFunction.StringToDate("HH:mm:ss",
dates[i]));
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
if (i == 0) {
calendar1.add(Calendar.HOUR_OF_DAY, hours);
calendar1.add(Calendar.MINUTE, minutes);
calendar1.add(Calendar.SECOND, seconds);
} else {
calendar2.add(Calendar.HOUR_OF_DAY, hours);
calendar2.add(Calendar.MINUTE, minutes);
calendar2.add(Calendar.SECOND, seconds);
}
}
long diffInMilis = calendar2.getTimeInMillis()
- calendar1.getTimeInMillis();
long diffInSecond = diffInMilis / 1000;
long diffInMinute = diffInMilis / (60 * 1000)% 60;
long diffInHour = diffInMilis / (60 * 60 * 1000);
System.out.println("diffInSecond==> " + diffInSecond);
System.out.println("diffInMinute==> " + diffInMinute);
System.out.println("diffInHour==> " + diffInHour);
}
我喜欢这样。但在这里,我输错了。
diffInSecond==> 67455
diffInMinute==> 44
diffInHour==> 18
答案 0 :(得分:1)
最后,我总结一下这个解决方案。但我不知道这是一个正确的方法。我得到了预期的OP。
private void test() {
String[] dates = { "01:00:06", "02:30:00", "05:00:09", "01:59:06",
"10:15:06" };
long totalSecs=0;
for(int i=0;i<dates.length;i++){
totalSecs+=GetSeconds(dates[i]);
}
long hours = totalSecs / 3600;
long minutes = (totalSecs % 3600) / 60;
long seconds = totalSecs % 60;
System.out.println("hours==> " + hours);
System.out.println("minutes==> " + minutes);
System.out.println("seconds==> " + seconds);
}
private long GetSeconds(String time){
String[] parts = time.split(":");
long totSec=0;
int hour=Integer.parseInt(parts[0]);
int min=Integer.parseInt(parts[1]);
int sec=Integer.parseInt(parts[2]);
totSec=(hour*3600)+(min*60)+sec;
return totSec;
}