@Test public void testParseDate() throws Exception { long testTime = 1422728245L; String testTimeString = "1422728245"; String testTimeMillisString = "1422728245697"; String testTimeGMTString = "2015/01/31-18:17:25 GMT+0"; String testTimeLocalNoTzString = new SimpleDateFormat("YYYY/MM/dd-HH:mm:ss").format(new Date(testTime*1000)); String testTimeEasternString = "2015/1/31-13:17:25-0500"; long computedNow = System.currentTimeMillis() / 1000L; long now = Utils.parseDate(Utils.NOW); long tenSecondsAgo = Utils.parseDate("10s-ago"); long lastMinute = Utils.parseDate("1m-ago"); long lastHour = Utils.parseDate("1h-ago"); long yesterday = Utils.parseDate("1d-ago"); Assert.assertEquals("Parsed now should equal computed now", computedNow, now, 5); Assert.assertEquals("Parsed 10s-ago should equal computed now - 10", tenSecondsAgo, now - 10, 5); Assert.assertEquals("Parsed 1m-ago should equal computed now - 60", lastMinute, now - Utils.SECONDS_PER_MINUTE, 5); Assert.assertEquals("Parsed 1h-ago should equal computed now - 3600", lastHour, now - Utils.SECONDS_PER_HOUR, 5); Assert.assertEquals("Parsed 1d-ago should equal computed now - (3600 * 24)", yesterday, now - Utils.SECONDS_PER_DAY, 5); Assert.assertEquals("Parsed now should equal computed now", computedNow, now, 5); Assert.assertEquals("Parsed now should equal computed now", computedNow, now, 5); Assert.assertEquals("Parsed now should equal computed now", computedNow, now, 5); Assert.assertEquals("Parsed GMT String should return appropriate time", testTime, Utils.parseDate(testTimeGMTString)); Assert.assertEquals("Parsed Local no TimeZone String should return appropriate time", testTime, Utils.parseDate(testTimeLocalNoTzString)); Assert.assertEquals("Parsed Eastern String should return appropriate time", testTime, Utils.parseDate(testTimeEasternString)); Assert.assertEquals("Parsed Epoch string should return appropriate time", testTime, Utils.parseDate(testTimeString)); Assert.assertEquals("Parsed Epoch millisecond string should return appropriate time", testTime, Utils.parseDate(testTimeMillisString)); }
public static long parseDate(String value) throws ParseException { String v = value.trim(); if (NOW.equals(v)) { return new Date().getTime() / 1000; } if (v.endsWith("-ago")) { return new Date().getTime() / 1000 - parseDuration(v.substring(0, v.length() - 4)); } if (v.indexOf('/') == -1) { long result = 0; try { result = Long.parseLong(v); if (result > HEURISTIC_EPOCH) { result /= 1000; } return result; } catch (NumberFormatException nfe) { } } try { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ssZ").parse(v) .getTime() / 1000; } catch (ParseException e) { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").parse(v) .getTime() / 1000; } }
Utils { public static long parseDate(String value) throws ParseException { String v = value.trim(); if (NOW.equals(v)) { return new Date().getTime() / 1000; } if (v.endsWith("-ago")) { return new Date().getTime() / 1000 - parseDuration(v.substring(0, v.length() - 4)); } if (v.indexOf('/') == -1) { long result = 0; try { result = Long.parseLong(v); if (result > HEURISTIC_EPOCH) { result /= 1000; } return result; } catch (NumberFormatException nfe) { } } try { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ssZ").parse(v) .getTime() / 1000; } catch (ParseException e) { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").parse(v) .getTime() / 1000; } } }
Utils { public static long parseDate(String value) throws ParseException { String v = value.trim(); if (NOW.equals(v)) { return new Date().getTime() / 1000; } if (v.endsWith("-ago")) { return new Date().getTime() / 1000 - parseDuration(v.substring(0, v.length() - 4)); } if (v.indexOf('/') == -1) { long result = 0; try { result = Long.parseLong(v); if (result > HEURISTIC_EPOCH) { result /= 1000; } return result; } catch (NumberFormatException nfe) { } } try { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ssZ").parse(v) .getTime() / 1000; } catch (ParseException e) { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").parse(v) .getTime() / 1000; } } private  Utils(); }
Utils { public static long parseDate(String value) throws ParseException { String v = value.trim(); if (NOW.equals(v)) { return new Date().getTime() / 1000; } if (v.endsWith("-ago")) { return new Date().getTime() / 1000 - parseDuration(v.substring(0, v.length() - 4)); } if (v.indexOf('/') == -1) { long result = 0; try { result = Long.parseLong(v); if (result > HEURISTIC_EPOCH) { result /= 1000; } return result; } catch (NumberFormatException nfe) { } } try { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ssZ").parse(v) .getTime() / 1000; } catch (ParseException e) { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").parse(v) .getTime() / 1000; } } private  Utils(); static Map<String, Object> makeError(String errorMessage, String errorCause, String errorPart); static Response getErrorResponse(String id, int status,
                                            String message, String context); static String createUuid(); static long parseDate(String value); static long parseDuration(String v); static String jsonStringFromObject(Object object); static ObjectMapper getObjectMapper(); }
Utils { public static long parseDate(String value) throws ParseException { String v = value.trim(); if (NOW.equals(v)) { return new Date().getTime() / 1000; } if (v.endsWith("-ago")) { return new Date().getTime() / 1000 - parseDuration(v.substring(0, v.length() - 4)); } if (v.indexOf('/') == -1) { long result = 0; try { result = Long.parseLong(v); if (result > HEURISTIC_EPOCH) { result /= 1000; } return result; } catch (NumberFormatException nfe) { } } try { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ssZ").parse(v) .getTime() / 1000; } catch (ParseException e) { return new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").parse(v) .getTime() / 1000; } } private  Utils(); static Map<String, Object> makeError(String errorMessage, String errorCause, String errorPart); static Response getErrorResponse(String id, int status,
                                            String message, String context); static String createUuid(); static long parseDate(String value); static long parseDuration(String v); static String jsonStringFromObject(Object object); static ObjectMapper getObjectMapper(); static final String NOW; static final String DEFAULT_START_TIME; static final String DEFAULT_END_TIME; static final String NOT_SPECIFIED; static final String CLIENT_ID; static final String ERRORS; static final String ERROR_MESSAGE; static final String ERROR_CAUSE; static final String ERROR_PART; static final String START; static final String END; static final String COUNT; static final double DEFAULT_DOWNSAMPLE_MULTIPLIER; static final int DAYS_PER_YEAR; static final int DAYS_PER_WEEK; static final int HOURS_PER_DAY; static final int MINUTES_PER_HOUR; static final int SECONDS_PER_MINUTE; static final int SECONDS_PER_HOUR; static final int SECONDS_PER_DAY; static final int SECONDS_PER_WEEK; static final int SECONDS_PER_YEAR; }