/** * Find the difference between two dates in hours * @param base date1 * @param forecast date2 * @return (forecast minus base) difference in hours */ public static double getOffsetInHours(CalendarDate base, CalendarDate forecast) { long diff = forecast.getDifferenceInMsecs(base); return diff / 1000.0 / 60.0 / 60.0; }
/** * Find the difference between two dates in hours * @param base date1 * @param forecast date2 * @return (forecast minus base) difference in hours */ public static double getOffsetInHours(CalendarDate base, CalendarDate forecast) { long diff = forecast.getDifferenceInMsecs(base); return diff / 1000.0 / 60.0 / 60.0; }
/** * Find the difference between two dates in hours * @param base date1 * @param forecast date2 * @return (forecast minus base) difference in hours */ public static double getOffsetInHours(CalendarDate base, CalendarDate forecast) { long diff = forecast.getDifferenceInMsecs(base); double result = diff / 1000.0 / 60.0 / 60.0; // LOOK why convert to double? precision may be lost ?? long testRoundoff = (long) ( result * 1000.0 * 60.0 * 60.0); if (diff != testRoundoff) System.out.println("HEY getOffsetInHours"); return result; }
public static int getOffset(CalendarDate refDate, CalendarDate cd, CalendarPeriod timeUnit) { long msecs = cd.getDifferenceInMsecs(refDate); return (int) Math.round(msecs / timeUnit.getValueInMillisecs()); }
/** * Subtract two dates, return difference in units of this period. * If not even, will round down and log a warning * @param start start date * @param end end date * @return difference in units of this period */ public int subtract(CalendarDate start, CalendarDate end) { long diff = end.getDifferenceInMsecs(start); int thislen = millisecs(); if ((diff % thislen != 0)) log.warn("roundoff error"); return (int) (diff / thislen); }
/** * Subtract two dates, return difference in units of this period. * If not even, will round down and log a warning * @param start start date * @param end end date * @return difference in units of this period */ public int subtract(CalendarDate start, CalendarDate end) { long diff = end.getDifferenceInMsecs(start); int thislen = millisecs(); if ((diff % thislen != 0)) log.warn("roundoff error"); return (int) (diff / thislen); }
/** * Subtract two dates, return difference in units of this period. * If not even, will round down and log a warning * @param start start date * @param end end date * @return difference in units of this period */ public int subtract(CalendarDate start, CalendarDate end) { long diff = end.getDifferenceInMsecs(start); int thislen = millisecs(); if ((diff % thislen != 0)) log.warn("roundoff error"); return (int) (diff / thislen); }
/** * Get difference between two calendar dates in given Field units * @param o other calendar date * @return (this minus o) difference in units of this Field */ 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(); }
for (Object coord : coords) { CalendarDate cd = (CalendarDate) coord; long msecs = cd.getDifferenceInMsecs(runDate); int val = (int) Math.round(msecs / duration); offsets.add(val);
public double makeOffsetFromRefDate( CalendarDate date) { if (isCalendarField) { if (date.equals(baseDate)) return 0.0; return date.getDifference(baseDate, periodField); } else { long msecs = date.getDifferenceInMsecs(baseDate); return msecs / period.getValueInMillisecs(); } }
private void testCalendar(Calendar cal, String s) { CalendarDateUnit cdu; try { cdu = CalendarDateUnit.withCalendar(cal, s); } catch (Exception e) { e.printStackTrace(); return; } System.out.printf("%s == %s unit (cal=%s)%n", s, cdu, cdu.getCalendar()); CalendarDate base = null; for (int i = 0; i < 13; i++) { CalendarDate cd = cdu.makeCalendarDate(i); if (base == null) base = cd; double diff = cd.getDifferenceInMsecs(base) * 1.0e-6; System.out.printf(" %d %s == %s diff = %f%n", i, cdu, CalendarDateFormatter.toDateTimeStringISO(cd), diff); } System.out.printf("%n"); }
extract.extract(sdata); CalendarDate date = extract.makeCalendarDate(); if (Math.abs(date.getDifferenceInMsecs(today)) > 1000L * 3600 * 24 * 100) { extract.makeCalendarDate();