javajodatime

Joda Period in year month day


i am using following code to get difference between 2 dates in year,month,day

tenAppDTO.getTAP_PROPOSED_START_DATE()=2009-11-01  
tenAppDTO.getTAP_PROPOSED_END_DATE()=2013-11-29                                                                         
ReadableInstant r=new DateTime(tenAppDTO.getTAP_PROPOSED_START_DATE());
ReadableInstant r1=new DateTime(tenAppDTO.getTAP_PROPOSED_END_DATE());
Period period = new Period(r, r1);  
period.normalizedStandard(PeriodType.yearMonthDay());
years =  period.getYears();
month=period.getMonths();
day=period.getDays();   
out.println("year is-:"+years+"month is -:"+ month+"days is -:"+ day);

by using above code i get the result year is-:4 month is -:0 days is -:0 but actual result is year is-:4 month is -:0 days is -:28

Please provide a solution


Solution

  • You may try to change

    Period period = new Period(r, r1); 
    

    with

    Period period = new Period(r, r1, PeriodType.yearMonthDay());
    

    You may try like this:

    tenAppDTO.getTAP_PROPOSED_START_DATE()=2009-11-01  
    tenAppDTO.getTAP_PROPOSED_END_DATE()=2013-11-29                                                                         
    ReadableInstant r=new DateTime(tenAppDTO.getTAP_PROPOSED_START_DATE());
    ReadableInstant r1=new DateTime(tenAppDTO.getTAP_PROPOSED_END_DATE());
    Period period = new Period(r, r1, PeriodType.yearMonthDay());   //Change here  
    period.normalizedStandard(PeriodType.yearMonthDay());
    years =  period.getYears();
    month=period.getMonths();
    day=period.getDays();   
    out.println("year is-:"+years+"month is -:"+ month+"days is -:"+ day);