Adding and subtracting dates. Java

Example:
11.09.2016 - 13.08.2013 = 29.0.3
11.09.2016 + 13.08.2013 = 24.5.4030

The input must be a string of the type

11.09.2016 - 13.08.2013

Output

29.0.3


My idea was to translate the input dates into milliseconds and perform operations. BUT when converting the result (from milliseconds to a date), it turns out to be heresy.

How can I add / subtract dates?

Author: Andrew.G, 2016-09-24

3 answers

The difference between two dates cannot be represented (correctly) as YY. MM. DD. You can count as suggested by Alexey Shimansky (the difference of years; the difference of months, if negative, we reduce by 1 the difference of years; then days), but the result will be meaningless.

The problem is as follows:

1) Take 4 dates: a) 31-01-2015, b) 01-03-16, c) 31-01-2016, d) 01-03-2016. If you subtract the dates (b-a and d-c) as you suggest, you will get the same result (0 years 1 month 1 day). But on in fact, there are 29 days between the dates in the first case, and 30 in the second.

2) Example from my comment: 2015.03.01 - 2015.01.31 and 2015.03.31 - 2015.03.02. The difference is the same here and there-29 days. And you will get different results.

In general, by subtracting one date from another, we lose the "binding" to a specific month and year. The result of subtracting two dates is simply a time interval that is not tied to a specific year. It can be expressed in seconds, minutes, or days, but it can't be expressed in months or years.

Sum of two dates it does not make any sense at all, although it can be quite correctly calculated by adding time since epoche and translating it back to the date.

 3
Author: andy.37, 2016-09-24 18:30:44

Java 8 already has ready-made classes for working with classes.

Adding dates

import java.time.LocalDate;

public class AdditionDate {

    public String getOperator() {
        return "+";
    }

    public void getResult(LocalDate firstDate, LocalDate secondDate) {
        LocalDate result = firstDate.plusYears(secondDate.getYear()).
                plusMonths(secondDate.getMonthValue()).plusDays(secondDate.getDayOfMonth());
        System.out.println(result);
    }
}

Subtracting dates

import java.time.LocalDate;
import java.time.Period;

public class SubtractionDate {

    public String getOperator() {
        return "-";
    }

    public void getResult(LocalDate firstDate, LocalDate secondDate) {
        Period period = Period.between(secondDate, firstDate);
        System.out.println(period.getYears() + "." + period.getMonths() + "." + period.getDays());
    }
}

It is better to do subtraction using Period, because if you use the minusYear()/minusMonth()/minusDat () operators, the difference is incorrectly calculated.
True, the output of the period will be of the type y. m. d -> 1.2.3

 3
Author: Andrew.G, 2016-10-12 18:03:55

Solution option via Calendar instance

public static void main(String[] args) {
    Calendar t2 = new GregorianCalendar(2016, 9, 26);
    Calendar t1 = new GregorianCalendar(2013, 8, 13);
    System.out.println(getDifference(t2, t1));
}

private static String getDifference(Calendar t1, Calendar t2) {
    StringBuilder result = new StringBuilder("> ");
    result.append(t1.get(Calendar.YEAR) > t2.get(Calendar.YEAR) ? t1.get(Calendar.YEAR) - t2.get(Calendar.YEAR)
            : t2.get(Calendar.YEAR) - t1.get(Calendar.YEAR));
    result.append(":");
    result.append(t1.get(Calendar.MONTH) > t2.get(Calendar.MONTH) ? t1.get(Calendar.MONTH) - t2.get(Calendar.MONTH)
            : t2.get(Calendar.MONTH) - t1.get(Calendar.MONTH));
    result.append(":");
    result.append(t1.get(Calendar.DATE) > t2.get(Calendar.DATE) ? t1.get(Calendar.DATE) - t2.get(Calendar.DATE)
            : t2.get(Calendar.DATE) - t1.get(Calendar.DATE));
    return result.append(" < ").toString();
}

It looks rich but it works well)

 1
Author: Peter Slusar, 2016-09-24 17:40:03