/** * @return Returns the scale. */ public int getScale() { return period.getScale(); }
/** * @return Returns the begin. */ public String getStringBegin() { String formatted = ""; if(period.getScale() == 0) formatted = df.get().format(period.getBegin()); return formatted; }
/** * @return Returns the end. */ public String getStringEnd() { String formatted = ""; if(period.getScale() == 0) formatted = df.get().format(period.getEnd()); return formatted; }
@Test public void scale() { Date now = new Date(); Calendar calBegin = Calendar.getInstance(); calBegin.setTime(now); Date end = calBegin.getTime(); calBegin.add(Calendar.HOUR, -4); Date begin = calBegin.getTime(); Period p = new Period(4); long deltaBegin = Math.abs(p.getBegin().getTime() - begin.getTime()); long deltaEnd = Math.abs(p.getEnd().getTime() + 1000 - end.getTime()); Assert.assertTrue("Delta end is too wide: " + deltaEnd, deltaEnd < 1000); Assert.assertTrue("Delta begin is too wide: " + deltaBegin, deltaBegin < 1000); Assert.assertEquals(new Integer(4), new Integer(p.getScale())); }
@Test public void previousScale() throws ParseException { Period p = new Period().previous(); long offsetDay = 86400 * 1000 - (p.getEnd().getTime() - p.getBegin().getTime()); Assert.assertTrue("offset to large: " + offsetDay, Math.abs(offsetDay) < 1100); Assert.assertEquals(0, p.getScale()); }
@Test public void nextScale() throws ParseException { Period p = new Period().next(); logger.trace(p.getBegin()); logger.trace(p.getEnd()); long offsetDay = 86400 * 1000 - (p.getEnd().getTime() - p.getBegin().getTime()); Assert.assertTrue("offset to large: " + offsetDay, Math.abs(offsetDay) < 1100); Assert.assertEquals(0, p.getScale()); }
@Test public void mixedFormat() throws ParseException { begin = fullISOFORMAT.parse("2007-01-01T00:00:00"); end = fullISOFORMAT.parse("2007-12-31T23:59:59"); Period p = new Period("2007-01-01T00:00", "2007-12-31T23:59"); Assert.assertEquals(p.getBegin(), begin); Assert.assertEquals(p.getEnd(), end); Assert.assertEquals(0, p.getScale()); }
@Test public void fullIsoFormat() throws ParseException { begin = fullISOFORMAT.parse("2007-01-01T00:00:00"); end = fullISOFORMAT.parse("2007-12-31T23:59:59"); Period p = new Period("2007-01-01T00:00:00", "2007-12-31T23:59:59"); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test public void spaceSeparator() throws ParseException { begin = fullISOFORMAT.parse("2007-02-01T00:15:31"); end = fullISOFORMAT.parse("2007-12-31T23:59:50"); Period p = new Period("2007-02-01 00:15:31", "2007-12-31 23:59:50"); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test public void nextFullWithOneSecond() throws ParseException { begin = fullISOFORMAT.parse("2007-03-03T00:00:00"); end = fullISOFORMAT.parse("2007-03-03T23:59:59"); Period p = new Period("2007-03-02 00:00:00", "2007-03-03T00:00:00").next(); logger.trace(p.getBegin()); logger.trace(p.getEnd()); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test public void StrictIsoFormat() throws ParseException { begin = strictISOFORMAT.parse("20070101T00:00:00+0000"); end = strictISOFORMAT.parse("20071231T23:59:59+0000"); Period p = new Period("20070101T00:00:00Z", "20071231T23:59:59Z"); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(0, p.getScale()); }
@Test public void shortFormat() throws ParseException { begin = fullISOFORMAT.parse("2007-01-01T00:00:00"); end = fullISOFORMAT.parse("2007-12-31T23:59:59"); Period p = new Period("2007-01-01", "2007-12-31"); Assert.assertEquals(p.getBegin(), begin); Assert.assertEquals(p.getEnd(), end); Assert.assertEquals(0, p.getScale()); }
@Test public void nextFull() throws ParseException { begin = fullISOFORMAT.parse("2007-03-03T00:00:00"); end = fullISOFORMAT.parse("2007-03-03T23:59:59"); Period p = new Period("2007-03-02 00:00:00", "2007-03-02 23:59:59").next(); logger.trace(p.getBegin()); logger.trace(p.getEnd()); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test public void fromLong() throws ParseException { begin = new Date(1000); end = new Date(100000); Period p = new Period("1000", "100000"); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test public void previousFull() throws ParseException { begin = fullISOFORMAT.parse("2007-03-01T00:00:00"); end = fullISOFORMAT.parse("2007-03-01T23:59:59"); Period p = new Period("2007-03-02 00:00:00", "2007-03-02 23:59:59").previous(); logger.trace(p.getBegin()); logger.trace(p.getEnd()); Assert.assertEquals(begin, p.getBegin()); Assert.assertEquals(end, p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test(expected = ParseException.class) public void invalid2() throws ParseException { Period p = new Period(" ", ""); Assert.assertNull(p.getBegin()); Assert.assertNull(p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test(expected = ParseException.class) public void invalid1() throws ParseException { Period p = new Period("a1", "2007-01"); Assert.assertNull(p.getBegin()); Assert.assertNull(p.getEnd()); Assert.assertEquals(0, p.getScale()); }
@Test(expected = ParseException.class) public void nullargs() throws ParseException { Period p = new Period(null, null); Assert.assertNull(p.getBegin()); Assert.assertNull(p.getEnd()); Assert.assertEquals(0, p.getScale()); }