TimeUtil { public static ZonedDateTime subtractOffset(ZonedDateTime time, String offset) { if (offset.startsWith("-")) { return addOffset(time, offset.substring(1)); } else { return addOffset(time, "-" + offset); } } private  TimeUtil(); static Instant lastInstant(Instant instant, Schedule schedule); static Instant previousInstant(Instant instant, Schedule schedule); static Instant nextInstant(Instant instant, Schedule schedule); static List<Instant> instantsInRange(Instant firstInstant, Instant lastInstant,
                                              Schedule schedule); static List<Instant> instantsInReversedRange(Instant firstInstant, Instant lastInstant,
                                                      Schedule schedule); static boolean isAligned(Instant instant, Schedule schedule); static ZonedDateTime addOffset(ZonedDateTime time, String offset); static ZonedDateTime subtractOffset(ZonedDateTime time, String offset); static Cron cron(Schedule schedule); static Instant offsetInstant(Instant origin, Schedule schedule, int offset);  }
@Test public void shouldSubtractNegativeOffset() { String offset = "-P1M3DT1H7M5S"; ZonedDateTime time = ZonedDateTime.parse("2017-02-25T09:14:16.22Z"); ZonedDateTime offsetTime = subtractOffset(time, offset); assertThat(offsetTime, is(ZonedDateTime.parse("2017-03-28T10:21:21.22Z"))); }
@Test public void shouldSubtractOffsetWithNoTime() { String offset = "P1M3D"; ZonedDateTime time = ZonedDateTime.parse("2017-02-25T08:07:11.22Z"); ZonedDateTime offsetTime = subtractOffset(time, offset); assertThat(offsetTime, is(ZonedDateTime.parse("2017-01-22T08:07:11.22Z"))); }
@Test public void shouldSubtractOffsetWithWeek() { String offset = "P2W"; ZonedDateTime time = ZonedDateTime.parse("2017-02-05T08:07:11.22Z"); ZonedDateTime offsetTime = subtractOffset(time, offset); assertThat(offsetTime, is(ZonedDateTime.parse("2017-01-22T08:07:11.22Z"))); }