public RelativeIso8601Date(final String dateStr) { /** * We must pass a date parsable {@link Iso8601Date#Iso8601Date(String)}; we will never actually read the passed value, so it doesn't * matter what it is. */ super(LAZY_NOW_LABEL.equals(dateStr) ? new Iso8601Date(new Date()).toString() : dateStr); doSetTimeNextQuery = LAZY_NOW_LABEL.equals(dateStr); }
private void truncateMilliseconds() { long time = getTime(); long mod = time % 1000; if (mod != 0) { super.setTime(time - mod); } } }
@Test public void equalityTest() { final String s = new Iso8601Date(new Date(12345)).toString(); final Iso8601Date iso8601Date = new Iso8601Date(s); final RelativeIso8601Date relativeIso8601Date = new RelativeIso8601Date(s); Assert.assertEquals(relativeIso8601Date.getTime(), iso8601Date.getTime(), DELTA_FOR_TIME); } }
/** Parse a String into an Iso8601 Date */ public Iso8601Date parseIso8601Date(String value) { return new Iso8601Date(value); }
/** * Returns a {@link String} representation of this date. * * @return An {@link Iso8601Date}-formatted string, or the value of {@link #LAZY_NOW_LABEL} if this is a "lazy now" instance. */ @Override public String toString() { return doSetTimeNextQuery ? LAZY_NOW_LABEL : super.toString(); }
@Test public void testMillisecondTruncation() { // Create a Date with milliseconds final Date now = new Date(); if (now.getTime() % 1000 == 0) { now.setTime(now.getTime() + 3); } Iso8601Date isoDate = new Iso8601Date(now); Assert.assertEquals(isoDate.getTime() % 1000, 0); Assert.assertEquals(isoDate.getTime() / 1000, now.getTime() / 1000); } }
@Override public boolean before(final Date when) { conditionallyUpdateTime(); return super.before(when); }
@Override public int compareTo(final Date anotherDate) { conditionallyUpdateTime(); return super.compareTo(anotherDate); }
@Override @Deprecated public int getDate() { conditionallyUpdateTime(); return super.getDate(); }
@Override public boolean equals(final Object obj) { conditionallyUpdateTime(); return super.equals(obj); }
@Override @Deprecated public int getDay() { conditionallyUpdateTime(); return super.getDay(); }
@Override public Object clone() { conditionallyUpdateTime(); return super.clone(); }
@Override public boolean after(final Date when) { conditionallyUpdateTime(); return super.after(when); }
@Test public void testNonLazyInstance() { final long time = new Iso8601Date(new Date(System.currentTimeMillis())).getTime(); // ISO strips off milliseconds // Test both constructor methods final List<RelativeIso8601Date> testDates = Arrays.<RelativeIso8601Date>asList( new RelativeIso8601Date(new Date(time)), new RelativeIso8601Date(new Iso8601Date(new Date(time)).toString()) ); for (final RelativeIso8601Date nonLazy : testDates) { Assert.assertFalse(nonLazy.toString().equals(RelativeIso8601Date.LAZY_NOW_LABEL)); Assert.assertEquals((double) nonLazy.getTime(), (double) time, DELTA_FOR_TIME); // Assert no exception thrown; this should be valid, because toString return an iso-looking date. new RelativeIso8601Date(nonLazy.toString()); } }
/** Parse a String into an Iso8601 Date */ public Iso8601Date parseIso8601Date(String value) { return new Iso8601Date(value); }
/** * Returns a {@link String} representation of this date. * * @return An {@link Iso8601Date}-formatted string, or the value of {@link #LAZY_NOW_LABEL} if this is a "lazy now" instance. */ @Override public String toString() { return doSetTimeNextQuery ? LAZY_NOW_LABEL : super.toString(); }
@Test public void testLazyInstance() { final RelativeIso8601Date lazy = RelativeIso8601Date.generateLazyNowInstance(); Assert.assertEquals(lazy.toString(), RelativeIso8601Date.LAZY_NOW_LABEL); Assert.assertEquals(lazy.toString(), RelativeIso8601Date.LAZY_NOW_LABEL); Assert.assertEquals(lazy.toString(), RelativeIso8601Date.LAZY_NOW_LABEL); Assert.assertEquals(lazy.getTime(), new Iso8601Date(new Date(System.currentTimeMillis())).getTime(), DELTA_FOR_TIME); // Assert no exception thrown; this should be valid, because toString should now return an iso-looking date. new Iso8601Date(lazy.toString()); }
@Override public boolean before(final Date when) { conditionallyUpdateTime(); return super.before(when); }
@Override public int compareTo(final Date anotherDate) { conditionallyUpdateTime(); return super.compareTo(anotherDate); }
@Override @Deprecated public int getDate() { conditionallyUpdateTime(); return super.getDate(); }