Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDate.plus Incorrect Answer

Java's LocalDate API seems to be giving the incorrect answer when calling plus(...) with a long Period, where I'm getting an off by one error. Am I doing something wrong here?

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Main
{
    public static void main(String[] args)
    {
        // Long Period
        LocalDate birthA = LocalDate.of(1965, Month.SEPTEMBER, 27);
        LocalDate eventA = LocalDate.of(1992, Month.MAY, 9);
        LocalDate halfA = eventA.plus(Period.between(birthA, eventA));
        System.out.println(halfA); // 2018-12-21 ????
        System.out.println(ChronoUnit.DAYS.between(birthA, eventA)); // 9721
        System.out.println(ChronoUnit.DAYS.between(eventA, halfA)); // 9722 ????

        // Short Period
        LocalDate birthB = LocalDate.of(2012, Month.SEPTEMBER, 10);
        LocalDate eventB = LocalDate.of(2012, Month.SEPTEMBER, 12);
        LocalDate halfB = eventB.plus(Period.between(birthB, eventB));
        System.out.println(halfB); // 2018-09-14
        System.out.println(ChronoUnit.DAYS.between(birthB, eventB)); // 2
        System.out.println(ChronoUnit.DAYS.between(eventB, halfB)); // 2
    }
}
like image 491
Daniel Centore Avatar asked May 10 '19 15:05

Daniel Centore


People also ask

How do I add something to LocalDate?

The plusDays() method of a LocalDate class in Java is used to add the number of specified day in this LocalDate and return a copy of LocalDate. For example, 2018-12-31 plus one day would result in 2019-01-01. This instance is immutable and unaffected by this method call.

How do I pass LocalDate as parameter in Postman?

format(("YYYY-MM-DD"))); {{$timestamp}} -> predefined variable gets the current timestamp, Since you need date the above one should work. use the parameter {{currentdate}} in your GET request.

Is LocalDate a data type?

LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value "2nd October 2007" can be stored in a LocalDate .


1 Answers

A Period is made of a number of years, months and days. In your case, Period.between(birthA, eventA) is 26 years, 7 months and 12 days.

If you add that to birthA, you get:

  • 1965 + 26 years -> 1991
  • September 1991 + 7 months -> April 1991
  • April 27, 1991 + 12 days -> May 9, 1992

Which works as expected.

If you apply the same calculation, starting from May 9, 1992, you get December 21, 2018.

If you want to add a certain number of days instead, you can't simply add the period (as years and months don't always have the same length). One option is to use ChonoUnit.DAYS.between instead:

LocalDate halfA = eventA.plusDays(ChronoUnit.DAYS.between(birthA, eventA));

That returns 2018-12-20 which I think is what you expected.

like image 181
assylias Avatar answered Sep 28 '22 08:09

assylias