Example :
September 11, 2016 - August 13, 2013 = 29.0.3
09/11/2016 + 08/13/2013 = 24.5.4030

Input should be a string of type

September 11, 2016 - August 13, 2013

At the exit

29.0.3


My idea was to translate the input dates in milliseconds and perform operations. BUT when converting the result (from milliseconds to the date) is heresy.

How can I add / subtract dates?

  • one
    What heresy is obtained? Can I see? - Alexey Shimansky
  • Alexey Shimansky, the beginning of the calculation in unix time is January 1, 1970, which means any date will start to run from this date - Ziens
  • @Ziens and why this info? - Alexey Shimansky
  • @ Alexey Shimansky, this will be heresy if you just translate - Ziens
  • 2
    The fact is that the difference of two dates is not a date, but a time interval. The sum of two dates is generally incomprehensible. - andy.37

3 answers 3

The difference between the two dates can not be represented (correctly) in the form of YY.MM.DD. It can be considered as suggested by Alexey Shimansky (difference of years; difference of months, if negative, decreasing by one 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 (bs and gs) as you propose, you will get the same result (0 years, 1 month, 1 day). But actually between dates in the first case 29 days, in the second - 30.

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

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

The sum of two dates is completely devoid of any meaning, although it can be quite correctly calculated by adding the time since epoche and translated back into the date.

  • In general, I agree with you, but for example, I counted on datacalc.ru and somehow they did it, and quite correctly - Ziens
  • @Ziens the result of subtracting 2 dates in the form of YY.MM.DD can make sense only in the case of binding to one of the two original dates. By itself, it is meaningless. - andy.37

Java 8 already has ready classes for working with classes.

Date addition

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

Subtract 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 through Period, because if you use the minusYear () / minusMonth () / minusDat () operators, then the difference is incorrectly considered.
True period output will be of type ymd -> 1.2.3

    Solution option through the 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(); } 

    looks rich but works out safely)

    • If you insert the date from the example, it turns out> 3: 1: 2 <, not 3: 0: 29 - Ziens
    • Check out what happens for the dates 2016/9/26 and 2013/10/13. - andy.37
    • @ andy.37,> 3: 1: 13 <, the online calculator shows what should be 2 years 11 months 13 days - Ziens
    • @ Ziens @ andy.37 - yes, in fact, the accumulation of the current month / year is taken into account - I will add - Peter Slusar