Java中如何对时间求和?

Phi*_*ffi 3 java jodatime

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

----------------------
| 活动 | 时间 |
 ----------------------
| 1 | 11:00:00 | 11:00:00
 -----------------------
| 2 | 12:00:00 | 12:00:00
 -----------------------
| 3 | 13:00:00 | 13:00:00
 -----------------------
| 总计 | 36:00:00 | 36:00:00
 ----------------------

我正在尝试对时间戳进行求和,如下所示:

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());
}
Run Code Online (Sandbox Code Playgroud)

该变量timestampsList是 的ArrayList ,全部遵循SimpleDateFormatString对象使用的模式。给定代码的问题是,我无法通过使用未来日期通知的模式中的一个小时来生成时间戳总和的值。SimpleDateFormat

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

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

pet*_*rov 5

我会自己解析这些字符串,将它们转换为
秒或毫秒,然后将它们相加。请参阅下面的答案 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

答案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;
    }
}
Run Code Online (Sandbox Code Playgroud)

答案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()));
    }

}
Run Code Online (Sandbox Code Playgroud)