0

I have a start DateTime and I need to check if current DatTime is exactly 1,2,3,4,5... years after start date. Does anyone know how to do this?

final DateTime start = formatter.parseDateTime("02.10.2015 09:00");
final DateTime end = formatter.parseDateTime("02.10.2016 08:00");

final Year years = Years.yearsBetween(start, end); //this isn't what i want but I have tried with Year object
quma
  • 5,233
  • 26
  • 80
  • 146
  • 1
    How would you handle leap years? Would March 1, 2017 and February 29, 2016 be considered as differing by exactly one year? You could use [Period](http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html) as per [this answer](http://stackoverflow.com/a/21668003/277106) and make sure that `days`, `weeks` and `months` are all 0. – Genti Saliu Feb 03 '16 at 18:06
  • It doesn't use JodaTime, but still: http://stackoverflow.com/a/2517875/277106 – Genti Saliu Feb 03 '16 at 18:39

1 Answers1

0

I removed the previously accepted answer, after reading about your requirements in the comments: You want to calculate the difference in exact calendar years and were unsure of how to handle leap years.

The requirements will be the following:

  • 2015-02-12 and 2016-02-12: difference 1 year
  • 2015-02-12 and 2017-02-12: difference 2 years
  • 2016-02-29 and 2018-02-28: difference 2 years
  • 2016-02-29 and 2020-02-29: difference 4 years

The above should cover all edge cases. It means that adding exactly one year to the leap day of a leap year (for example February 29, 2016) will return February 28 (2017 as per the example). Most frameworks (Joda and .NET, which I checked) handle it this way.

One would expect that adding one year to the leap day (for ex. February 29, 2016) should yield March 1 instead of February 28 because 2016 has 366 days and the day which would have been February 29 in the next year is March 1, but it doesn't seem to be the case in the different implementations I saw.

I would suggest you stick to the frameworks' way of doing things.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");
final LocalDate start = formatter.parseLocalDate("29.02.2016");
final LocalDate end = formatter.parseLocalDate("28.02.2019");

Period period = new Period(start, end, PeriodType.yearMonthDay());
boolean exactlyMultipleOfYears = period.getDays() == 0 && period.getMonths() == 0 && period.getYears() > 0;
Genti Saliu
  • 2,643
  • 4
  • 23
  • 43
  • So actually I only need to now, if the start datetime is **2014-12-12**, that it is the xxxx-12-12 at any year. This would be my requirement, but I dont know if I should compare day and month or if there is a better solution. – quma Feb 03 '16 at 18:23
  • Can you state your requirement more precisely, especially considering leap years (see my comment below your question)? Then we could provide a good answer. Otherwise, if you *technically* need to check that the difference between two dates is exactly a multiple of years, then my answer so far is the way to go. – Genti Saliu Feb 03 '16 at 18:27
  • Thanks for the response. My requirement is that I have to add holiday requirement after a year passed by - its a real problem with the leap years. I dont know how to solve this. – quma Feb 03 '16 at 20:26
  • Okay, I will post a solution later for this. Basically **2016-02-12** and **2017-02-12** is exactly one year, regardless of 2016 being a leap year. **2016-02-29** and **2017-03-01** is also exactly one year (the day which would have been Feb 29 in 2017 is actually March 1). These examples should cover all scenarios. – Genti Saliu Feb 04 '16 at 07:46