Search code examples
javadatejodatimeperiod

Joda-Time – Remove one Period from another


I am trying to remove a Period of time from another Period of time in Joda-Time, but it does not work.

Period p = new Period(new LocalDate("2014-01-01"), new LocalDate("2014-02-20"),PeriodType.days())

From this remove another Period:

Period minusD = new Period(new LocalDate(dates[0]), new LocalDate(dates[1]),PeriodType.days());
p.minus(minusD);

and now just get the days from the first period.

p.getDays();

I tried using the MutablePeriod as well, but that did not seem to work either.


Solution

  • minus does not change the given Period object, it creates a new object that's the difference between the two Periods:

    Period p = new Period(/*something*/);
    Period minusD = new Period(/*period to subtract*/);
    Period result = p.minus(minusD);
    System.out.println (result.getDays());