Java添加日期格式为dd:HH:mm:ss

问题描述 投票:1回答:4

我有三个日期作为String对象,格式为:dd:HH:mm:ss

  • 00:1:9:14
  • 00:3:10:4
  • 00:3:39:49

如何在Java中添加这些日期以获取总和(00:7:59:07)?

示例代码:

SimpleDateFormat sdf = new SimpleDateFormat("dd:HH:mm:ss");
Date d1 = sdf.parse("00:1:9:14");
Date d2 = sdf.parse("00:3:10:4");
Date d3 = sdf.parse("00:3:39:49");

System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
Date d = new Date(d1.getTime() + d2.getTime() + d3.getTime());

System.out.println(d);

输出(错误):

Wed Dec 31 01:09:14 IST 1969
Wed Dec 31 03:10:04 IST 1969
Wed Dec 31 03:39:49 IST 1969
Sun Dec 28 20:59:07 IST 1969
java date simpledateformat addition
4个回答
2
投票

dd格式包括月份中的一天。因此,如果您使用00(或Java SimpleDateFormat,因为它也包含一个月中的某天),则Date的值将下溢。相反,请解析时间部分并自己进行数学计算。

例如,您可以使用TimePartdayshoursminutes等创建类seconds,>

static class TimePart {
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;

    static TimePart parse(String in) {
        if (in != null) {
            String[] arr = in.split(":");
            TimePart tp = new TimePart();
            tp.days = ((arr.length >= 1) ? Integer.parseInt(arr[0]) : 0);
            tp.hours = ((arr.length >= 2) ? Integer.parseInt(arr[1]) : 0);
            tp.minutes = ((arr.length >= 3) ? Integer.parseInt(arr[2]) : 0);
            tp.seconds = ((arr.length >= 4) ? Integer.parseInt(arr[3]) : 0);
            return tp;
        }
        return null;
    }

    public TimePart add(TimePart a) {
        this.seconds += a.seconds;
        int of = 0;
        while (this.seconds >= 60) {
            of++;
            this.seconds -= 60;
        }
        this.minutes += a.minutes + of;
        of = 0;
        while (this.minutes >= 60) {
            of++;
            this.minutes -= 60;
        }
        this.hours += a.hours + of;
        of = 0;
        while (this.hours >= 24) {
            of++;
            this.hours -= 24;
        }
        this.days += a.days + of;
        return this;
    }

    @Override
    public String toString() {
        return String.format("%02d:%02d:%02d:%02d", days, hours, minutes,
                seconds);
    }
}

然后是您的测试用例

public static void main(String[] args) {
    try {
        TimePart d1 = TimePart.parse("00:1:9:14");
        TimePart d2 = TimePart.parse("00:3:10:4");
        TimePart d3 = TimePart.parse("00:3:39:49");
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
        TimePart d4 = d1.add(d2).add(d3);
        System.out.println(d4);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

而且似乎可以正确执行加法,如

00:01:09:14
00:03:10:04
00:03:39:49
00:07:59:07

1
投票

以上总和是算术加法,因此您需要参考--d0(默认纪元)。日期类要提防很多问题...


1
投票
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");    

String s1 = "01:02:03";
String s2 = "10:12:13";

Date d1 = format.parse(s1);
Date d2 = format.parse(s2);

int sec = d1.getSeconds() + d2.getSeconds();
int min = d1.getMinutes() + d2.getMinutes();
int hr = d1.getHours() + d2.getHours();

Time sum = new Time(hr, min, sec);
System.out.println(sum); // Output: 11:14:16

0
投票
private static String addTimes(String time1, String time2) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(dateFormat.parse(time1));
    c2.setTime(dateFormat.parse(time2));

    c1.add(Calendar.HOUR, c2.get(Calendar.HOUR));
    c1.add(Calendar.MINUTE, c2.get(Calendar.MINUTE));
    c1.add(Calendar.SECOND, c2.get(Calendar.SECOND));
    return dateFormat.format(c1.getTime());
}

addTimes("1:9:14", "3:10:4");    
© www.soinside.com 2019 - 2024. All rights reserved.