TimeParser { public List<Date> parse(String value) throws ParseException { if (value == null) { return Collections.emptyList(); } value = value.trim(); if (value.length() == 0) { return Collections.emptyList(); } final List<Date> dates = new ArrayList<Date>(); if (value.indexOf(',') >= 0) { String[] listDates = value.split(","); for (int i=0; i<listDates.length; i++) { dates.add(getDate(listDates[i].trim())); } return dates; } String[] period = value.split("/"); if (period.length == 1) { if(value.equals("current")) { dates.add(Calendar.getInstance(UTC_TZ).getTime()); } else { dates.add(getDate(value)); } return dates; } if (period.length == 3) { final Date begin = getDate(period[0]); final Date end = getDate(period[1]); final long millisIncrement = parsePeriod(period[2]); final long startTime = begin.getTime(); final long endTime = end.getTime(); long time; int j = 0; while ((time = j * millisIncrement + startTime) <= endTime) { Calendar calendar = Calendar.getInstance(UTC_TZ); calendar.setTimeInMillis(time); dates.add(calendar.getTime()); j++; } return dates; } throw new ParseException("Invalid time parameter: " + value, 0); } TimeParser(); List<Date> parse(String value);  }
@Test public void testParserOnCurrentTime() throws ParseException, InterruptedException { long now = System.currentTimeMillis(); Thread.sleep(20); final String timeInstant = "current"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertTrue(now < time.get(0).getTime()); }
@Test public void testParserOnNullTime() throws ParseException{ List<Date> time = PARSER.parse(null); assertTrue(time.isEmpty()); }
@Test public void testParserOnTimeInstantFormat1() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-01-01T00:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat2() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "201110"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-01T00:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat3() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-01T00:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat4() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T00:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat5() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T00:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat6() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat7() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat8() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat9() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:00:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat10() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T1011"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat11() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat12() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T1011"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat13() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat14() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T1011Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat15() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat16() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T1011Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat17() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:00.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat18() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T101120"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat19() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11:20"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat20() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T101120"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat21() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11:20"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat22() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T101120Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat23() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11:20Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat24() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T101120Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat25() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11:20Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.000Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat26() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T101120666"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat27() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11:20.666"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat28() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T101120666"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat29() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11:20.666"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat30() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T101120666Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat31() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "20111010T10:11:20.666Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat32() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T101120666Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimeInstantFormat33() throws ParseException { final SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); df.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInstant = "2011-10-10T10:11:20.666Z"; List<Date> time = PARSER.parse(timeInstant); assertEquals(1, time.size()); assertEquals("2011-10-10T10:11:20.666Z",df.format(time.get(0))); }
@Test public void testParserOnTimePeriod() throws ParseException { final String timeInterval = "2011-10-10T10:11:12.000Z/2011-10-10T14:11:12.000Z/PT1H"; List<Date> time = PARSER.parse(timeInterval); assertEquals(5, time.size()); assertEquals(1318241472000l, time.get(0).getTime()); assertEquals(1318241472000l + (3600 * 1000 * 4), time.get(time.size()-1).getTime()); }
@Test public void testParserOnDayPeriod() throws ParseException { final String timeInterval = "2011-10-10T10:11:12.000Z/2011-10-14T10:11:12.000Z/P2D"; List<Date> time = PARSER.parse(timeInterval); assertEquals(3, time.size()); assertEquals(1318241472000l, time.get(0).getTime()); assertEquals(1318241472000l + (3600 * 1000 * 48), time.get(1).getTime()); }
@Test @Ignore public void testParserOnCurrentTime() throws ParseException, InterruptedException { TimeParser parser = new TimeParser(); long now = System.currentTimeMillis(); Thread.sleep(20); final String timeInstant = "current"; List<Date> time = parser.parse(timeInstant); assertEquals(1, time.size()); assertTrue(now < time.get(0).getTime()); }
@Test @Ignore public void testParserOnNullTime() throws ParseException{ TimeParser parser = new TimeParser(); List<Date> time = parser.parse(null); assertTrue(time.isEmpty()); }
@Test @Ignore public void testParserOnTimeInstant() throws ParseException { TimeParser parser = new TimeParser(); final String timeInstant = "2011-10-10T10:11:12.000Z"; List<Date> time = parser.parse(timeInstant); assertEquals(1, time.size()); assertEquals(1318241472000l, time.get(0).getTime()); }
@Test @Ignore public void testParserOnTimePeriod() throws ParseException { TimeParser parser = new TimeParser(); final String timeInterval = "2011-10-10T10:11:12.000Z/2011-10-10T14:11:12.000Z/PT1H"; List<Date> time = parser.parse(timeInterval); assertEquals(5, time.size()); assertEquals(1318241472000l, time.get(0).getTime()); assertEquals(1318241472000l + (3600 * 1000 * 4), time.get(time.size()-1).getTime()); }
@Test @Ignore public void testParserOnDayPeriod() throws ParseException { TimeParser parser = new TimeParser(); final String timeInterval = "2011-10-10T10:11:12.000Z/2011-10-14T10:11:12.000Z/P2D"; List<Date> time = parser.parse(timeInterval); assertEquals(3, time.size()); assertEquals(1318241472000l, time.get(0).getTime()); assertEquals(1318241472000l + (3600 * 1000 * 48), time.get(1).getTime()); }