1
public static void main(String args[]) throws ParseException{


    String string = "May 2, 2016";
    DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
    Date date = format.parse(string);
    System.out.println(date);
    DateTime dateTime = new DateTime(date);
    DateTime currentDate = new DateTime(Calendar.getInstance().getTime());
    System.out.println(Calendar.getInstance().getTime());
    Period p = new Period(dateTime, currentDate);
    System.out.println(p.getYears());
    System.out.println(p.getMonths());
    System.out.println(p.getDays());

}

}

Result for days is 1

expected considering today is june 10 2016 it should be 8

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40

2 Answers2

2

Nothing is wrong here, you get 1 because 8 days is one week and one day. If you want to get 8 for the "day", you have to compute it back from the week part (i.e. week * 7 + day).

2

To get what you expect you should use another PeriodType: PeriodType.yearMonthDay().

Period p = new Period(dateTime, currentDate, PeriodType.yearMonthDay());

Currently your code uses standard (default) PeriodType, which breaks the period into years, months, weeks, days, hours, minutes, seconds, millis.

Roman
  • 6,486
  • 2
  • 23
  • 41