Hello everyone,
I have an example which give strange result :
This is the code :
MutablePeriod mp = new MutablePeriod();
DateTime dtEff1 = new DateTime(2004, 7, 20, 0, 0, 0, 0);
DateTime dtEch1 = new DateTime(2006, 4, 28, 0, 0, 0, 0);
mp.add(new Period(dtEff1, dtEch1));
DateTime dtEff2 = new DateTime(1999, 7, 20, 0, 0, 0, 0);
DateTime dtEch2 = new DateTime(2004, 7, 20, 0, 0, 0, 0);
mp.add(new Period(dtEff2, dtEch2));
DateTime dtEff3 = new DateTime(1986, 1, 9, 0, 0, 0, 0);
DateTime dtEch3 = new DateTime(1988, 4, 13, 0, 0, 0, 0);
mp.add(new Period(dtEff3, dtEch3));
DateTime dtEff4 = new DateTime(1984, 7, 24, 0, 0, 0, 0);
DateTime dtEch4 = new DateTime(1984, 9, 13, 0, 0, 0, 0);
mp.add(new Period(dtEff4, dtEch4));
DateTime dtEff5 = new DateTime(1979, 7, 17, 0, 0, 0, 0);
DateTime dtEch5 = new DateTime(1984, 7, 24, 0, 0, 0, 0);
mp.add(new Period(dtEff5, dtEch5));
System.out.println(mp.toString());
And the result : P13Y13M4W11D
I think result with 13M is strange.
What do you think of this ?
Greets
RATM
RE: Strange result with Period
By: Stephen Colebourne (scolebourneProject AdminAccepting Donations) - 2006-10-30 02:56
This is correct behaviour with Joda-Time.
The operation of adding a period to another period simply adds each field to the matching field. Thus P7M + P7M = P14M.
If you want to resolve the 13 months in your example back to 1 year and 1 month, then you need to convert the period to an interval with a fixed start date, and then back to a period.
The reason for this behaviour is that the length of fields can vary (eg. a day may be 23, 24 or 25 hours long when DST is considered). As such, Joda-Time cannot just normalize the fields without the risk of an inaccurate answer.
Partager