Citation:
I have another (last example)
This is the code
DateTimedtDateEff = new DateTime(1989, 7, 25, 0, 0, 0, 0);
DateTime dtDateEch = new MutableDateTime(1994, 7, 18, 0, 0, 0, 0);
dtDateEch.addDays(1);
p = new Period(dtDateEff, dtDateEch);
System.out.println(p.toString());
And the result : P4Y11M3W3D
I think that the result should be P4Y11M3W4D :
25/07/1989 to 31/07/1989 => 7 days
01/08/1989 to 30/06/1994 => 4 years 11 months
01/07/1994 to 18/07/1994 => 18 days
Total = 4 years 11 monts 25 days (P4Y11M3W4D)
Firstly i suppose the end date given to the Period period constructor is exclude (and the start date is include).
How explain this difference of one day and how work the calcul of period in Joda Time.
Thanks for your help.
RATM
RE: Another question about calcul Period
By: Stephen Colebourne (scolebourneProject AdminAccepting Donations) - 2006-11-01 14:29
Joda-Time calculates period by starting with the largest field and working to the smallest field.
DateTime a = new DateTime(1989, 7, 25, 0, 0, 0, 0);
DateTime b = new DateTime(1994, 7, 18, 0, 0, 0, 0);
Given these we ask:
- how many whole years are there between the two dates => 4
- add 4 years to the start date - 1993-07-25
- how many whole months are there between the new temp date and the end date => 11
- add 11 months to the start date - 1994-06-25
- how many whole weeks are there between the new temp date and the end date => 3
- add 3 weeks to the start date - 1994-07-16
- how many whole days are there between the new temp date and the end date => 2
Thus - P4Y11M3W2D
Citation:
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.