Sum two dates in Java
calendar, date, java
Solution
If you are using the Date object, you can just do:
Date d1 = ...
Date d2 = ...
long sum = d1.getTime() + d2.getTime();
Date sumDate = new Date(sum);
The code uses the `.getTime()` method that returns the number of milliseconds since the epoch. Needless to say the `Date` class has a lot of problems and should be avoided when possible.
Do you want to sum other types instead?
Update: for `Calendar`, I would do the following (based on javadocs):
Calendar c1 = ...
Calendar c2 = ...
long sum = c1.getTimeInMillis() + c2.getTimeInMillis();
Calendar sumCalendar = (Calendar)c1.clone();
sumCalendar.setTimeInMillis(sum);
UPDATED: As Steve stated, this works if the Date you presented here assumes that the second date is with respect to the Java epoch. If you do want to start with year "0", then you need to account for that (by subtracting your epoch time).
Problem
How can I add two dates in Java? Example: The sum of "2010-01-14 19:16:17" "0000-10-03 01:10:05" would result in "2010-11-17 20:26:22". I know how to do it using Calendar and adding field by field. Is any other way to sum them all (year/month/day/hour/minute/second) at once?