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

patrick zhang patrick.zhang at oracle.com
Thu Feb 28 01:12:23 PST 2013


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