Java add dates of format dd:HH:mm:ss

本秂侑毒 提交于 2020-01-14 03:28:11

问题


I have three dates as String objects in the format: dd:HH:mm:ss

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

How do I add these dates in Java to get the sum (00:7:59:07)?

Sample code:

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

Output(wrong):

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

回答1:


The dd format includes the day of the month. So your value of 00 will underflow if you use SimpleDateFormat (or Java Date because it also includes a day of the month). Instead, parse your time parts and do the math yourself.

For example, you could create a class TimePart with days, hours, minutes and seconds like

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

Then your test-cases like

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

And it seems to perform the addition correctly like

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



回答2:


The above sum is arithmetic addition so you need a ref --here d0 (default epoch). Date class has a lot of problems beware...

SimpleDateFormat sdf = new SimpleDateFormat("dd:HH:mm:ss");
Date d0 = sdf.parse("00:00:00:00"); // ref 
Date d1 = sdf.parse("00:01:09:14");
Date d2 = sdf.parse("00:03:10:04");
Date d3 = sdf.parse("00:03:39:49");

System.out.println(d0);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
Date d = new Date(d1.getTime() + d2.getTime() + d3.getTime() - 2 * d0.getTime()); // impt

System.out.println(d);

Note:- Date addition is not an easy task, think twice.




回答3:


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


来源:https://stackoverflow.com/questions/26786098/java-add-dates-of-format-ddhhmmss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!