在java中添加时间

在java中添加时间,java,datetime,time,Java,Datetime,Time,我正在从数据库中获取以下时间行 TimeWorked 09:05:25 09:30:15 10:15:01 08:19:49 09:17:40 现在我想用java总结一下我们在一起的时间,得到46:28:10。如何用java总结这些小时 感谢您的回复如果在您的项目中可能的话,我建议您使用JodaTime和类。这将比使用标准的Java日历/日期API容易得多 下面是使用Period类的另一个示例: PeriodFormatter hoursMinutesSecondsFormatter =

我正在从数据库中获取以下时间行

TimeWorked
09:05:25
09:30:15
10:15:01
08:19:49
09:17:40
现在我想用java总结一下我们在一起的时间,得到46:28:10。如何用java总结这些小时


感谢您的回复

如果在您的项目中可能的话,我建议您使用JodaTime和类。这将比使用标准的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

如果可能,我建议您在项目中使用JodaTime和类。这将比使用标准的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
int-hour=0;
int分钟=0;
for(){
字符串[]行时间=行[i]。拆分(“:”;
hour+=Integer.parseInt(行时间[0]);
分钟+=整数.parseInt(行时[1]);
}
小时+=分钟/60;
分钟%=60;
字符串结果=小时+“:”+分钟

启动行循环,在循环中获取每一行,然后继续

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
int-hour=0;
int分钟=0;
for(){
字符串[]行时间=行[i]。拆分(“:”;
hour+=Integer.parseInt(行时间[0]);
分钟+=整数.parseInt(行时[1]);
}
小时+=分钟/60;
分钟%=60;
字符串结果=小时+“:”+分钟
String[]timeWorked={“09:05:25”,“09:30:15”}
整小时=0;
整数分钟=0;
整数秒=0;
for(int i=0;i
不要忘记捕获可能从parseInt抛出的numberformatceptions

String[]timeWorked={“09:05:25”,“09:30:15”}
整小时=0;
整数分钟=0;
整数秒=0;
for(int i=0;i

别忘了捕获可能从parseInt抛出的NumberFormatException

您可以发布您尝试使用日历的内容吗?我知道为什么它不起作用?你写了什么代码来解决你的问题?什么是开始时间?什么是结束时间?我建议大家看一看,然后把这个问题提出来:非常感谢你们的回复。你们可以发布你们用日历做过什么吗?我知道为什么它不起作用?你写了什么代码来解决你的问题?什么是开始时间?什么是结束时间?我建议大家看一看,然后在上面引用这个问题:非常感谢您的回复。对未来的评论:这个答案适用于Java的所有版本,直到当前的稳定版本7。从Java 8开始,在标准Java中会有一些非常类似于JodaTime的东西。Java 8 time API是通过设计的,可以在中找到。对未来的评论:这个答案适用于当前稳定版本7之前的所有Java版本。从Java 8开始,在标准Java中会有一些非常类似于JodaTime的东西。Java 8 time API是通过设计的,将在中找到。您错过了一件事。如果在执行mod 60时分钟数小于10,该怎么办?我修复了它,感谢您错过了一件事。如果在执行mod 60时分钟数小于10,该怎么办?我修复了它,谢谢您怎样