### Question:
DateUtil { public static Duration duration(Calendar fromDate, Calendar toDate) { Integer monthResult = 0; Integer dayResult = 0; Integer yearResult = 0; Integer hourResult = 0; Integer hourIncrement = 0; Integer minutesResult = 0; Integer minutesIncrement = 0; Integer increment = 0; if (toDate.get(Calendar.MINUTE) < fromDate.get(Calendar.MINUTE)) { minutesIncrement = 1; minutesResult = (toDate.get(Calendar.MINUTE) + 60) - fromDate.get(Calendar.MINUTE); } else { minutesIncrement = 0; minutesResult = toDate.get(Calendar.MINUTE) - fromDate.get(Calendar.MINUTE); } if (toDate.get(Calendar.HOUR_OF_DAY) < (fromDate.get(Calendar.HOUR_OF_DAY) + minutesIncrement)) { hourResult = (toDate.get(Calendar.HOUR_OF_DAY) + 24) - fromDate.get(Calendar.HOUR_OF_DAY); hourIncrement = 1; } else { hourResult = toDate.get(Calendar.HOUR_OF_DAY) - fromDate.get(Calendar.HOUR_OF_DAY); hourIncrement = 0; } if( toDate.get(Calendar.DATE) < fromDate.get(Calendar.DATE) ){ increment = MONTH[fromDate.get(Calendar.MONTH)-1]; } if (increment == -1) { Boolean leapYear = fromDate.getActualMaximum(Calendar.DAY_OF_YEAR) > 365; if (leapYear) { increment = 29; } else { increment = 28; } } if (increment != 0) { dayResult = (toDate.get(Calendar.DATE) + increment) - fromDate.get(Calendar.DATE); increment = 1; } else { dayResult = toDate.get(Calendar.DATE) - fromDate.get(Calendar.DATE); increment = 0; } if( toDate.get(Calendar.MONTH) < (fromDate.get(Calendar.MONTH) + increment) ){ monthResult = (toDate.get(Calendar.MONTH)+12) - fromDate.get(Calendar.MONTH); increment = 1; } else { monthResult = toDate.get(Calendar.MONTH) - fromDate.get(Calendar.MONTH); increment = 0; } yearResult = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR)+1); Duration d = new Duration(); d.setYears(yearResult); d.setDays(dayResult); d.setMonths(monthResult); d.setHours(hourResult); d.setMinutes(minutesResult); return d; }  static JSONObject setCreationTime(JSONObject jo, Integer days); static JSONObject setExpiryTime(JSONObject jo, Integer days); static JSONObject setExpiryTimeWithHours(JSONObject jo, Integer hours); static JSONObject addDate(JSONObject jo, String attrName, Date d); static Date addDays(Date date, Integer days); static Date addDays(Integer days); static Date addHours(Date date, Integer hours); static Date addHours(Integer hours); static Date getDate(JSONObject json); synchronized static String toUTCFormat(Date d); static Date toUTCFormat(String d); static XMLGregorianCalendar toXmlGregorian(Date d); static Date fromXmlGregorian(XMLGregorianCalendar xmlCal); static Boolean hasDateSubObject(Object object); static Duration duration(Calendar fromDate, Calendar toDate); static final SimpleDateFormat ServiceDateFormat; static final SimpleDateFormat ISODateFormat; static final SimpleDateFormat UTCISODateFormat; }### Answer:
@Test public void testDuration(){ Calendar fromDate = Calendar.getInstance(); fromDate.set(2012, 05, 31); Calendar toDate = Calendar.getInstance(); toDate.set(2014, 01, 02, 23, 15); System.out.println(DateUtil.duration(fromDate, toDate)); Assert.assertEquals("1 days 7 months 1 years", DateUtil.duration(fromDate, toDate).toString()); }