TimeCalculator { public static double getTimeBetween(final ZonedDateTime date1, final ZonedDateTime date2, final DayCount dayCount, final Calendar calendar) { ArgumentChecker.notNull(date1, "date1"); ArgumentChecker.notNull(date1, "date2"); ArgumentChecker.notNull(dayCount, "day count"); final ZonedDateTime rebasedDate2 = date2.withZoneSameInstant(date1.getZone()); final boolean timeIsNegative = date1.isAfter(rebasedDate2); if (!timeIsNegative) { return dayCount.getDayCountFraction(date1, rebasedDate2, calendar); } return -1.0 * dayCount.getDayCountFraction(rebasedDate2, date1, calendar); } private  TimeCalculator(); static double getTimeBetween(final ZonedDateTime date1, final ZonedDateTime date2, final DayCount dayCount, final Calendar calendar); static double getTimeBetween(final ZonedDateTime date1, final ZonedDateTime date2, final DayCount dayCount); static double getTimeBetween(final ZonedDateTime date1, final ZonedDateTime date2); static double[] getTimeBetween(final ZonedDateTime[] date1, final ZonedDateTime[] date2); static double[] getTimeBetween(final ZonedDateTime date1, final ZonedDateTime[] date2); static double getTimeBetween(final LocalDate date1, final LocalDate date2); static double getTimeBetween(final ZonedDateTime zdt1, final LocalDate date2); static double getTimeBetween(final LocalDate date1, final ZonedDateTime zdt2);  }
@Test public void sameInstant() { final ZonedDateTime now = ZonedDateTime.now(); assertEquals(0.0, TimeCalculator.getTimeBetween(now, now)); }
@Test public void sameDay() { final ZonedDateTime midday = LocalDate.now().atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); final ZonedDateTime midnight = LocalDate.now().atTime(LocalTime.MIDNIGHT).atZone(ZoneOffset.UTC); final double yearFraction = TimeCalculator.getTimeBetween(midday, midnight); assertEquals(0.0, yearFraction, TOLERANCE); }
@Test public void sameDay2() { final ZonedDateTime midday = LocalDate.now().atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); final ZonedDateTime midnight = LocalDate.now().atTime(LocalTime.MIDNIGHT).atZone(ZoneOffset.UTC); final double yearFraction = TimeCalculator.getTimeBetween(midnight, midday); assertEquals(0.0, yearFraction, TOLERANCE); }
@Test public void sameTimeDifferentLocalDates() { final ZonedDateTime midnightLondon = LocalDateTime.of(2012, 03, 12, 0, 0).atZone(ZoneOffset.UTC); final ZonedDateTime sevenNewYork = LocalDateTime.of(2012, 03, 11, 19, 0).atZone(ZoneId.of("EST")); assertTrue(midnightLondon.isEqual(sevenNewYork)); final double yearFraction = TimeCalculator.getTimeBetween(sevenNewYork, midnightLondon); assertEquals(0.0, yearFraction, TOLERANCE); }
@Test public void sameTimeDifferentLocalDates2() { final ZonedDateTime date1 = LocalDateTime.of(2013, 9, 24, 0, 0).atZone(ZoneId.of("Europe/London")); final ZonedDateTime date2 = LocalDateTime.of(2013, 9, 24, 9, 2, 45, 936000000).atZone(ZoneOffset.UTC); final double yearFraction = TimeCalculator.getTimeBetween(date1, date2); assertEquals("TimeCalculator", 0.0, yearFraction, TOLERANCE); }
@Test public void normal() { final ZonedDateTime date1 = DateUtils.getUTCDate(2010, 8, 18); final ZonedDateTime date2 = DateUtils.getUTCDate(2010, 8, 21); final double time = TimeCalculator.getTimeBetween(date1, date2); final double timeExpected = 3.0 / 365.0; assertEquals("TimeCalculator: normal days", timeExpected, time, TOLERANCE); }
@Test public void array() { final ZonedDateTime date1 = DateUtils.getUTCDate(2010, 8, 18); final ZonedDateTime date2 = DateUtils.getUTCDate(2010, 8, 21); final ZonedDateTime[] dateArray1 = new ZonedDateTime[] {date1, date2 }; final ZonedDateTime[] dateArray2 = new ZonedDateTime[] {date2, date1 }; final double[] timeCalculated = TimeCalculator.getTimeBetween(dateArray1, dateArray2); final double timeExpected = 3.0 / 365.0; assertArrayEquals("TimeCalculator: normal days array", new double[] {timeExpected, -timeExpected }, timeCalculated, TOLERANCE); final double[] timeCalculated2 = TimeCalculator.getTimeBetween(date1, dateArray2); assertArrayEquals("TimeCalculator: normal days array", new double[] {timeExpected, 0.0 }, timeCalculated2, TOLERANCE); }
@Test(enabled = false) public void plat4725() { ZoneId gmt = ZoneId.of("GMT"); ZoneId london = ZoneId.of("+01:00"); final ZonedDateTime date1 = ZonedDateTime.of(2013, 9, 24, 0, 0, 1, 0, london); final ZonedDateTime date2 = ZonedDateTime.of(2013, 9, 24, 9, 2, 45,936, gmt); final double time12 = TimeCalculator.getTimeBetween(date1, date2); final double time21 = TimeCalculator.getTimeBetween(date2, date1); assertEquals("TimeCalculator: across midnight", -1 * time12, time21, TOLERANCE); }