@Test public void testGetDifference() { CalendarDate baseDate = CalendarDate.of(null, 2000, 1, 2, 3, 4, 5); CalendarDate diffDate = CalendarDate.of(null, 2001, 2, 3, 4, 5, 6); assertThat(diffDate.getDifference(baseDate, Field.Year)).isEqualTo(1); assertThat(diffDate.getDifference(baseDate, Field.Month)).isEqualTo(13); assertThat(diffDate.getDifference(baseDate, Field.Day)).isEqualTo(398); assertThat(diffDate.getDifference(baseDate, Field.Hour)).isEqualTo(9553); assertThat(diffDate.getDifference(baseDate, Field.Minute)).isEqualTo(573181); assertThat(diffDate.getDifference(baseDate, Field.Second)).isEqualTo(34390861); assertThat(diffDate.getDifference(baseDate, Field.Millisec)).isEqualTo(34390861000L); CalendarDate diffDate2 = CalendarDate.of(null, 2001, 1, 2, 3, 4, 5); assertThat(diffDate2.getDifference(baseDate, Field.Year)).isEqualTo(1); assertThat(diffDate2.getDifference(baseDate, Field.Month)).isEqualTo(12); assertThat(diffDate2.getDifference(baseDate, Field.Day)).isEqualTo(366); assertThat(diffDate2.getDifference(baseDate, Field.Hour)).isEqualTo(366 * 24); assertThat(diffDate2.getDifference(baseDate, Field.Minute)).isEqualTo(366 * 24 * 60); assertThat(diffDate2.getDifference(baseDate, Field.Second)).isEqualTo(366 * 24 * 60 * 60); assertThat(diffDate2.getDifference(baseDate, Field.Millisec)).isEqualTo(31622400000L); CalendarDate diffDate3 = CalendarDate.of(null, 2000, 1, 2, 3, 4, 5); assertThat(diffDate3.getDifference(baseDate, Field.Year)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Month)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Day)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Hour)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Minute)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Second)).isEqualTo(0); assertThat(diffDate3.getDifference(baseDate, Field.Millisec)).isEqualTo(0); }
public long getDifference(CalendarDate o, CalendarPeriod.Field fld) { switch (fld) { case Millisec: return getDifferenceInMsecs(o); case Second: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_SECOND); case Minute: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_MINUTE); case Hour: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_HOUR); case Day: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_DAY); case Month: int tmonth = getFieldValue(CalendarPeriod.Field.Month); int omonth = o.getFieldValue(CalendarPeriod.Field.Month); int years = (int) this.getDifference(o, CalendarPeriod.Field.Year); return tmonth - omonth + 12 * years; case Year: int tyear = getFieldValue(CalendarPeriod.Field.Year); int oyear = o.getFieldValue(CalendarPeriod.Field.Year); return tyear - oyear; } return dateTime.getMillis() - o.dateTime.getMillis(); }
CalendarDate implements Comparable<CalendarDate> { public long getDifference(CalendarDate o, CalendarPeriod.Field fld) { switch (fld) { case Millisec: return getDifferenceInMsecs(o); case Second: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_SECOND); case Minute: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_MINUTE); case Hour: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_HOUR); case Day: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_DAY); case Month: int tmonth = getFieldValue(CalendarPeriod.Field.Month); int omonth = o.getFieldValue(CalendarPeriod.Field.Month); int years = (int) this.getDifference(o, CalendarPeriod.Field.Year); return tmonth - omonth + 12 * years; case Year: int tyear = getFieldValue(CalendarPeriod.Field.Year); int oyear = o.getFieldValue(CalendarPeriod.Field.Year); return tyear - oyear; } return dateTime.getMillis() - o.dateTime.getMillis(); } }
CalendarDate implements Comparable<CalendarDate> { public long getDifference(CalendarDate o, CalendarPeriod.Field fld) { switch (fld) { case Millisec: return getDifferenceInMsecs(o); case Second: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_SECOND); case Minute: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_MINUTE); case Hour: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_HOUR); case Day: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_DAY); case Month: int tmonth = getFieldValue(CalendarPeriod.Field.Month); int omonth = o.getFieldValue(CalendarPeriod.Field.Month); int years = (int) this.getDifference(o, CalendarPeriod.Field.Year); return tmonth - omonth + 12 * years; case Year: int tyear = getFieldValue(CalendarPeriod.Field.Year); int oyear = o.getFieldValue(CalendarPeriod.Field.Year); return tyear - oyear; } return dateTime.getMillis() - o.dateTime.getMillis(); } CalendarDate(Calendar cal, DateTime dateTime); }
CalendarDate implements Comparable<CalendarDate> { public long getDifference(CalendarDate o, CalendarPeriod.Field fld) { switch (fld) { case Millisec: return getDifferenceInMsecs(o); case Second: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_SECOND); case Minute: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_MINUTE); case Hour: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_HOUR); case Day: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_DAY); case Month: int tmonth = getFieldValue(CalendarPeriod.Field.Month); int omonth = o.getFieldValue(CalendarPeriod.Field.Month); int years = (int) this.getDifference(o, CalendarPeriod.Field.Year); return tmonth - omonth + 12 * years; case Year: int tyear = getFieldValue(CalendarPeriod.Field.Year); int oyear = o.getFieldValue(CalendarPeriod.Field.Year); return tyear - oyear; } return dateTime.getMillis() - o.dateTime.getMillis(); } CalendarDate(Calendar cal, DateTime dateTime); static CalendarDate present(); static CalendarDate present(Calendar cal); static CalendarDate of(Calendar cal, int year, int monthOfYear, int dayOfMonth, int hourOfDay,
      int minuteOfHour, int secondOfMinute); static CalendarDate withDoy(Calendar cal, int year, int doy, int hourOfDay, int minuteOfHour,
      int secondOfMinute); static CalendarDate of(java.util.Date date); static CalendarDate of(long msecs); static CalendarDate of(Calendar cal, long msecs); @Nullable static CalendarDate parseUdunitsOrIso(String calendarName, String isoOrUdunits); static CalendarDate parseISOformat(String calendarName, String isoDateString); static CalendarDate parseUdunits(String calendarName, String udunits); Calendar getCalendar(); long getMillis(); @Override int compareTo(CalendarDate o); boolean isAfter(CalendarDate o); boolean isBefore(CalendarDate o); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); String getTimeUnits(); int getHourOfDay(); int getFieldValue(CalendarPeriod.Field fld); int getDayOfMonth(); @Deprecated CalendarDate add(double value, CalendarPeriod.Field unit); CalendarDate add(CalendarPeriod period); CalendarDate subtract(CalendarPeriod period); CalendarDate truncate(CalendarPeriod.Field fld); java.util.Date toDate(); long getDifferenceInMsecs(CalendarDate o); long getDifference(CalendarDate o, CalendarPeriod.Field fld); GregorianCalendar toGregorianCalendar(); }
CalendarDate implements Comparable<CalendarDate> { public long getDifference(CalendarDate o, CalendarPeriod.Field fld) { switch (fld) { case Millisec: return getDifferenceInMsecs(o); case Second: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_SECOND); case Minute: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_MINUTE); case Hour: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_HOUR); case Day: return (long) (getDifferenceInMsecs(o) / MILLISECS_IN_DAY); case Month: int tmonth = getFieldValue(CalendarPeriod.Field.Month); int omonth = o.getFieldValue(CalendarPeriod.Field.Month); int years = (int) this.getDifference(o, CalendarPeriod.Field.Year); return tmonth - omonth + 12 * years; case Year: int tyear = getFieldValue(CalendarPeriod.Field.Year); int oyear = o.getFieldValue(CalendarPeriod.Field.Year); return tyear - oyear; } return dateTime.getMillis() - o.dateTime.getMillis(); } CalendarDate(Calendar cal, DateTime dateTime); static CalendarDate present(); static CalendarDate present(Calendar cal); static CalendarDate of(Calendar cal, int year, int monthOfYear, int dayOfMonth, int hourOfDay,
      int minuteOfHour, int secondOfMinute); static CalendarDate withDoy(Calendar cal, int year, int doy, int hourOfDay, int minuteOfHour,
      int secondOfMinute); static CalendarDate of(java.util.Date date); static CalendarDate of(long msecs); static CalendarDate of(Calendar cal, long msecs); @Nullable static CalendarDate parseUdunitsOrIso(String calendarName, String isoOrUdunits); static CalendarDate parseISOformat(String calendarName, String isoDateString); static CalendarDate parseUdunits(String calendarName, String udunits); Calendar getCalendar(); long getMillis(); @Override int compareTo(CalendarDate o); boolean isAfter(CalendarDate o); boolean isBefore(CalendarDate o); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); String getTimeUnits(); int getHourOfDay(); int getFieldValue(CalendarPeriod.Field fld); int getDayOfMonth(); @Deprecated CalendarDate add(double value, CalendarPeriod.Field unit); CalendarDate add(CalendarPeriod period); CalendarDate subtract(CalendarPeriod period); CalendarDate truncate(CalendarPeriod.Field fld); java.util.Date toDate(); long getDifferenceInMsecs(CalendarDate o); long getDifference(CalendarDate o, CalendarPeriod.Field fld); GregorianCalendar toGregorianCalendar(); static final CalendarDate UNKNOWN; static final double MILLISECS_IN_SECOND; static final double MILLISECS_IN_MINUTE; static final double MILLISECS_IN_HOUR; static final double MILLISECS_IN_DAY; static final double MILLISECS_IN_YEAR; static final double MILLISECS_IN_MONTH; }