我从数据库中获取时间行如下
TimeWorked
09:05:25
09:30:15
10:15:01
08:19:49
09:17:40
现在我想在 java 中总结时间以获得46:28:10。我尝试了一些使用 Calendar 类的东西,但它没有奏效。如何在java中总结这些时间
谢谢您的回复
如果可能在您的项目中,我建议您使用 JodaTime 和Duration
类。这将比使用标准 Java 日历/日期 API 容易得多。
这是另一个使用 Period 类的示例:
PeriodFormatter hoursMinutesSecondsFormatter =
new PeriodFormatterBuilder().appendHours().appendSeparator(":")
.appendMinutes().appendSeparator(":").appendSeconds().toFormatter();
Period period1 = hoursMinutesSecondsFormatter.parseMutablePeriod("09:05:25").toPeriod();
Period period2 = hoursMinutesSecondsFormatter.parseMutablePeriod("09:30:15").toPeriod();
System.out.println(period1.plus(period2).toString(hoursMinutesSecondsFormatter));
版画:18:35:40
开始你的行循环让每一行进入循环并继续
int hour = 0;
int minute = 0;
for(<your row loop>) {
String[] rowtime= row[i].split(":");
hour += Integer.parseInt(rowtime[0]);
minute += Integer.parseInt(rowtime[1]);
}
hour += minute / 60;
minute %= 60;
String result = hour + ":" + minute
String[] timeWorked = {"09:05:25", "09:30:15"}
int hours = 0;
int minutes = 0;
int seconds = 0;
for(int i = 0; i < timeWorked.length; i++) {
hours += Integer.parseInt(timeWorked.split(":")[0]);
minutes += Integer.parseInt(timeWorked.split(":")[1]);
seconds += Integer.parseInt(timeWorked.split(":")[2]);
}
// Now you have your hours minutes and seconds all added up and all you have to do is do some math similar to what shreyansh jogi said to calculate in hours minutes and seconds.
不要忘记捕获可能从 parseInt 抛出的 NumberFormatExceptions