在java中添加两个字符串时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21560353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:23:03  来源:igfitidea点击:

Add two String Times in java

java

提问by junaidp

I have two String times

我有两个字符串时间

1:30:00
1:35:00

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

有没有一种简单的方法来添加这两个时间并获得一个应该是什么的新时间 3:05:00

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

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

采纳答案by Hot Licks

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

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

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

And convert back:

并转换回来:

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;
}

回答by Girish

Use SimpleDateFormat to parse the Strings then you can add the hours minutes and seconds

使用 SimpleDateFormat 解析字符串然后你可以添加小时分钟和秒

something like

就像是

 SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
  d1 = df.parse("1:30:00");
  d2 = df.parse("1:35:00");

Long sumtime= d1.getTime()+d2.getTime();

you can see this here as well it looks like possible duplicate of #####

你也可以在这里看到它看起来可能是#####的重复

or if you want to use Calender API, then you can also do it using Calender API, then u can do something like

或者如果你想使用 Calender API,那么你也可以使用 Calender API 来做,然后你可以做类似的事情

Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
     Calendar cTotal = Calendar.getInstance(); 
    cTotal.add(c1.get(Calendar.YEAR), c2.get(Calendar.YEAR));
    cTotal.add(c1.get(Calendar.MONTH) + 1)), c2.get(Calendar.MONTH) + 1)); // Months are zero-based!
    cTotal.add(c1.get(Calendar.DATE), c2.get(Calendar.DATE));

回答by peter.petrov

Just sum them as you sum numbers in 1st-2nd grades, going backwards through them.
Also make sure you move over digits to higher register when needed (i.e. not
always when reaching 10 but when reaching 24 or 60 for hours/minutes).
So I suggest you code this algorithm yourself.

只需在对 1 至 2 年级的数字求和时对它们进行求和,然后倒退即可。
还要确保在需要时将数字移到更高的寄存器(即并非
总是在达到 10 时,而是在达到 24 或 60 小时/分钟时)。
所以我建议你自己编写这个算法。

回答by Kick

String time1="0:01:30";
String time2="0:01:35";

SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Date date1 = timeFormat.parse(time1);
Date date2 = timeFormat.parse(time2);

long sum = date1.getTime() + date2.getTime();

String date3 = timeFormat.format(new Date(sum));
System.out.println("The sum is "+date3);

Ouput : The sum is 00:03:05

输出: The sum is 00:03:05