Reputation: 4062
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
Upvotes: 1
Views: 190
Reputation: 6657
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.
Upvotes: 2
Reputation:
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).
Upvotes: 2