[threeten-dev] incompatibility issues between MinguoDate.minus(Period) and MinguoDate.periodUntil(MinguoDate)

roger riggs roger.riggs at oracle.com
Thu Feb 28 06:26:16 PST 2013


Hi Patrick,

The basis for the observation is due to human expectations about plus and
minus on dates.  Adding or subtracting days works fine.
But adding or subtracting months or years has to deal with months and years
of different lengths.  If the day would not be valid for the resulting 
month it is
adjusted to be in the expected month.

Calendar dates are not linear and sequential so the property of
plusMonth() followed by minusMonth(n) cannot be guaranteed to return to 
the original date.

The javadoc can be more forceful in warning of this behavior but cannot 
change the
basic condition.

PeriodUntil(date) is well described and produces an accurate difference 
between the two dates.
In this example, the unexpected behavior is not in PeriodUntil but in 
date1.minus(1,1,1);

The properties of calendar arithmetic are not the same as those for 
ordinal numbers.
There is no way to make them the same.

Roger


On 2/28/2013 4:12 AM, patrick zhang wrote:
> The sample code for LocalDate looks as below:
> =======================
>        LocalDate date1 = LocalDate.of(1970, 1, 1) ;
>        Period p = Period.of(1, 1, 1) ;
>        LocalDate date2 = date1.minus(p) ;          LocalDate date3 = 
> date2.plus(p) ;  //should be same with date1?
>
>        System.out.println(p + "--" + date2.periodUntil(date1)) ; 
> //should be same with p? different
>        System.out.println(date1 + "--" + date3) ;   //different
> ======================
>
> Output:
> ======================
> P1Y1M1D--P1Y1M2D
> 1970-01-01--1969-12-31
> =====================
>
> The same Period object has different meaning on different instant. 
> Actually it is the difference between Period and Duration. Duration 
> always means one accurate period.
> But in my option, such kind of design will bring trouble to 
> application programmers.
> Maybe it is better to return Duration for all periodUntil() method to 
> avoid conflict.
>
> Regards
> Patrick
>
> On 2013-2-28 16:33, patrick zhang wrote:
>> The same problem happens for LocalDate also.
>> I guess it is caused by difference between Period of Duration. It 
>> works as expected when I use Duration to replace Period to define one 
>> accurate period.
>>
>> Regards
>> Patrick
>>
>> On 2013-2-28 16:25, patrick zhang wrote:
>>> Hi Team,
>>>
>>> As you know, MinguoDate.minus(Period) will return one new MinguoDate 
>>> while MinguoDate.periodUntil(MinguoDate) will calculate the period 
>>> between 2 date.
>>> so I think:
>>> if
>>>    minguodate2 = minguodate1.minus(period) ;
>>> then
>>>    minguodate2.periodUntil(minguodate1) = period.
>>>
>>> I write sample code to check it. It looks sometime it works but 
>>> sometimes it fails.
>>> You can see date3 and date4 work as expected while data1 and date2 
>>> look strange.
>>>
>>> I know it is caused by different length of months. And Period class 
>>> will do something to match it. But I am really a bit confused by the 
>>> output. Is it correct behavior?
>>>
>>> Code:
>>> =============================
>>>         MinguoDate date1 = MinguoDate.of(1970, 1, 1) ;
>>>        Period p = Period.of(1, 1, 1) ;
>>>        MinguoDate date2 = date1.minus(p) ; System.out.println(p) ;
>>>        System.out.println(date2.periodUntil(date1)) ;
>>>              System.out.println("**************") ;
>>>        MinguoDate date3 = MinguoDate.of(1970, 5, 3) ;
>>>        MinguoDate date4 = date3.minus(p) ; System.out.println(p) ;
>>>        System.out.println(date4.periodUntil(date3)) ;
>>> ===============================
>>>
>>> Output:
>>> ===============================
>>> P1Y1M1D
>>> P1Y1M2D
>>> **************
>>> P1Y1M1D
>>> P1Y1M1D
>>> ===============================
>>>
>>> Regards
>>> Patrick
>>>
>>


More information about the threeten-dev mailing list