在java中添加两个字符串时间 [英] Add two String Times in java

查看:239
本文介绍了在java中添加两个字符串时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串时间

1:30:00
1:35:00

是否有一种简单的方法可以添加这两次并获得一个新的时间,这应该是
3:05:00

Is there a simple way to add these two times and get a new time which should be something 3:05:00?

我想在客户端这样做,所以如果我可以避免任何日期liabraries

I want to do this at client side , so if i can avoid any date liabraries

推荐答案

请记住,您可以将小时/分钟/秒的int值转换为单个int,如下所示:

Keep in mind that you can convert int values for hours/minutes/seconds to a single int like this:

int totalSeconds = ((hours * 60) + minutes) * 60 + seconds;

并转换回来:

int hours = totalSeconds / 3600;  // Be sure to use integer arithmetic
int minutes = ((totalSeconds) / 60) % 60;
int seconds = totalSeconds % 60;

或者你可以按如下方式逐步进行算术:

Or you can do arithmetic piecemeal as follows:

int totalHours = hours1 + hours2;
int totalMinutes = minutes1 + minutes2;
int totalSeconds = seconds1 + seconds2;
if (totalSeconds >= 60) {
  totalMinutes ++;
  totalSeconds = totalSeconds % 60;
}
if (totalMinutes >= 60) {
  totalHours ++;
  totalMinutes = totalMinutes % 60;
}

这篇关于在java中添加两个字符串时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆