gpt4 book ai didi

java - Java中如何对时间求和?

转载 作者:行者123 更新时间:2023-12-01 22:41:07 25 4
gpt4 key购买 nike

我正在编写一个报告,该报告计算其中的数据总和,其中一些数据是时间戳,例如:

 ----------------------|  Activity |   Time   | ----------------------|     1     | 11:00:00 | -----------------------|     2     | 12:00:00 | -----------------------|     3     | 13:00:00 | -----------------------| Total     | 36:00:00 | ----------------------

I'm trying to sum timestamps as below:

final DateFormat dt = new SimpleDateFormat("HH:mm:ss");
final Calendar c = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
c.setTimeInMillis(0);
for (final String t : timestampsList) {
c.add(Calendar.MILLISECOND, (int) dt.parse(t).getTime());
}

变量timestampsList是一个ArrayList String 的,全部遵循 SimpleDateFormat 使用的模式目的。给定代码的问题是,我无法通过使用相同的 SimpleDateFormat 生成时间戳总和的值,我得到的是 future 日期通知的模式中的一个小时。

我也看过Joda Time Duration类,但我不熟悉这个库,而且我不知道我是否走在正确的道路上,可以引导我找到正确的答案。

有谁知道如何使用J2SE或Joda Time来处理它?

最佳答案

我会自己解析这些字符串,将它们转换为
秒或毫秒并将它们相加。请参阅下面的答案 2。

答案1

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


public class Test051 {

public static void main(String[] args) throws Exception {
String pt = "1970-01-01-";
ArrayList<String> timestampsList = new ArrayList<String>();
timestampsList.add("01:00:05");
timestampsList.add("01:00:05");
timestampsList.add("10:00:05");
final DateFormat dt = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
final Calendar sum = Calendar.getInstance();
sum.setTimeInMillis(0);

long tm0 = new SimpleDateFormat("yyyy-MM-dd").parse(pt).getTime();

System.out.println("tm0 = " + tm0);

for (final String t : timestampsList) {
// System.out.println(dt.parse(pt + t).getTime());
Date x = dt.parse(pt + t);
// System.out.println(x.getTime());
sum.add(Calendar.MILLISECOND, (int)x.getTime());
sum.add(Calendar.MILLISECOND, (int)-tm0);
}

long tm = sum.getTime().getTime();
System.out.println("tm = " + tm);

tm = tm / 1000;


long hh = tm / 3600;
tm %= 3600;
long mm = tm / 60;
tm %= 60;
long ss = tm;
System.out.println(format(hh) + ":" + format(mm) + ":" + format(ss));
}

private static String format(long s){
if (s < 10) return "0" + s;
else return "" + s;
}
}

答案2

import java.util.ArrayList;

public class Test051 {

public static void main(String[] args) throws Exception {
ArrayList<String> timestampsList = new ArrayList<String>();
timestampsList.add("01:00:05");
timestampsList.add("01:00:05");
timestampsList.add("10:00:05");

long tm = 0;
for (String tmp : timestampsList){
String[] arr = tmp.split(":");
tm += Integer.parseInt(arr[2]);
tm += 60 * Integer.parseInt(arr[1]);
tm += 3600 * Integer.parseInt(arr[0]);
}

long hh = tm / 3600;
tm %= 3600;
long mm = tm / 60;
tm %= 60;
long ss = tm;
System.out.println(format(hh) + ":" + format(mm) + ":" + format(ss));
}

private static String format(long s){
if (s < 10) return "0" + s;
else return "" + s;
}
}

答案3

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Test051 {

public static void main(String[] args) throws Exception {
ArrayList<String> timestampsList = new ArrayList<String>();
timestampsList.add("01:00:00");
timestampsList.add("02:00:00");
timestampsList.add("03:00:00");
timestampsList.add("04:00:00");
timestampsList.add("02:00:00");
timestampsList.add("04:00:00");

Date dt0 = new SimpleDateFormat("yyyy-MM-dd").parse("1970-01-01");

// Check very carefully the output of this one.
System.out.println(dt0.getTime());

final DateFormat dt = new SimpleDateFormat("HH:mm:ss");
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(0);
for (final String t : timestampsList) {
c.add(Calendar.MILLISECOND, (int) dt.parse(t).getTime());
c.add(Calendar.MILLISECOND, (int)-dt0.getTime());
}

// We need to add this back. This is basically the time zone offset.
c.add(Calendar.MILLISECOND, (int)dt0.getTime());

System.out.println(c.getTime().getTime());
System.out.println(c.getTimeInMillis());

System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime()));
System.out.println(new SimpleDateFormat("HH:mm:ss").format(c.getTime()));
}

}

关于java - Java中如何对时间求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com