@Test public void getNow() { Instant ts = TimeParser.getInstant("now"); Assert.assertTrue("Failed to obtain Timestamp corresponding to now ", ts != null && ts instanceof Instant); }
public static Instant getInstant(String time) { if (time.equalsIgnoreCase("now")) { return Instant.now(); } else { int quantity = 0; String unit = ""; Pattern lastNUnitsPattern = Pattern .compile( "last\\s*(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks).*", Pattern.CASE_INSENSITIVE); Matcher lastNUnitsMatcher = lastNUnitsPattern.matcher(time); while (lastNUnitsMatcher.find()) { quantity = "".equals(lastNUnitsMatcher.group(1)) ? 1 : Integer .valueOf(lastNUnitsMatcher.group(1)); unit = lastNUnitsMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } Pattern nUnitsAgoPattern = Pattern .compile( "(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks)\\s*ago", Pattern.CASE_INSENSITIVE); Matcher nUnitsAgoMatcher = nUnitsAgoPattern.matcher(time); while (nUnitsAgoMatcher.find()) { quantity = "".equals(nUnitsAgoMatcher.group(1)) ? 1 : Integer .valueOf(nUnitsAgoMatcher.group(1)); unit = nUnitsAgoMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(); } }
TimeParser { public static Instant getInstant(String time) { if (time.equalsIgnoreCase("now")) { return Instant.now(); } else { int quantity = 0; String unit = ""; Pattern lastNUnitsPattern = Pattern .compile( "last\\s*(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks).*", Pattern.CASE_INSENSITIVE); Matcher lastNUnitsMatcher = lastNUnitsPattern.matcher(time); while (lastNUnitsMatcher.find()) { quantity = "".equals(lastNUnitsMatcher.group(1)) ? 1 : Integer .valueOf(lastNUnitsMatcher.group(1)); unit = lastNUnitsMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } Pattern nUnitsAgoPattern = Pattern .compile( "(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks)\\s*ago", Pattern.CASE_INSENSITIVE); Matcher nUnitsAgoMatcher = nUnitsAgoPattern.matcher(time); while (nUnitsAgoMatcher.find()) { quantity = "".equals(nUnitsAgoMatcher.group(1)) ? 1 : Integer .valueOf(nUnitsAgoMatcher.group(1)); unit = nUnitsAgoMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(); } } }
TimeParser { public static Instant getInstant(String time) { if (time.equalsIgnoreCase("now")) { return Instant.now(); } else { int quantity = 0; String unit = ""; Pattern lastNUnitsPattern = Pattern .compile( "last\\s*(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks).*", Pattern.CASE_INSENSITIVE); Matcher lastNUnitsMatcher = lastNUnitsPattern.matcher(time); while (lastNUnitsMatcher.find()) { quantity = "".equals(lastNUnitsMatcher.group(1)) ? 1 : Integer .valueOf(lastNUnitsMatcher.group(1)); unit = lastNUnitsMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } Pattern nUnitsAgoPattern = Pattern .compile( "(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks)\\s*ago", Pattern.CASE_INSENSITIVE); Matcher nUnitsAgoMatcher = nUnitsAgoPattern.matcher(time); while (nUnitsAgoMatcher.find()) { quantity = "".equals(nUnitsAgoMatcher.group(1)) ? 1 : Integer .valueOf(nUnitsAgoMatcher.group(1)); unit = nUnitsAgoMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(); } }  }
TimeParser { public static Instant getInstant(String time) { if (time.equalsIgnoreCase("now")) { return Instant.now(); } else { int quantity = 0; String unit = ""; Pattern lastNUnitsPattern = Pattern .compile( "last\\s*(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks).*", Pattern.CASE_INSENSITIVE); Matcher lastNUnitsMatcher = lastNUnitsPattern.matcher(time); while (lastNUnitsMatcher.find()) { quantity = "".equals(lastNUnitsMatcher.group(1)) ? 1 : Integer .valueOf(lastNUnitsMatcher.group(1)); unit = lastNUnitsMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } Pattern nUnitsAgoPattern = Pattern .compile( "(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks)\\s*ago", Pattern.CASE_INSENSITIVE); Matcher nUnitsAgoMatcher = nUnitsAgoPattern.matcher(time); while (nUnitsAgoMatcher.find()) { quantity = "".equals(nUnitsAgoMatcher.group(1)) ? 1 : Integer .valueOf(nUnitsAgoMatcher.group(1)); unit = nUnitsAgoMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(); } }  static Duration getDuration(String time); static TimeInterval getTimeInterval(String time); static TimeInterval getTimeInterval(String start, String end); static Instant getInstant(String time); }
TimeParser { public static Instant getInstant(String time) { if (time.equalsIgnoreCase("now")) { return Instant.now(); } else { int quantity = 0; String unit = ""; Pattern lastNUnitsPattern = Pattern .compile( "last\\s*(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks).*", Pattern.CASE_INSENSITIVE); Matcher lastNUnitsMatcher = lastNUnitsPattern.matcher(time); while (lastNUnitsMatcher.find()) { quantity = "".equals(lastNUnitsMatcher.group(1)) ? 1 : Integer .valueOf(lastNUnitsMatcher.group(1)); unit = lastNUnitsMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } Pattern nUnitsAgoPattern = Pattern .compile( "(\\d*)\\s*(min|mins|hour|hours|day|days|week|weeks)\\s*ago", Pattern.CASE_INSENSITIVE); Matcher nUnitsAgoMatcher = nUnitsAgoPattern.matcher(time); while (nUnitsAgoMatcher.find()) { quantity = "".equals(nUnitsAgoMatcher.group(1)) ? 1 : Integer .valueOf(nUnitsAgoMatcher.group(1)); unit = nUnitsAgoMatcher.group(2).toLowerCase(); switch (unit) { case "min": case "mins": return Instant.now().minus(Duration.ofMinutes(quantity)); case "hour": case "hours": return Instant.now().minus(Duration.ofHours(quantity)); case "day": case "days": return Instant.now().minus(Duration.ofHours(quantity * 24)); case "week": case "weeks": return Instant.now().minus( Duration.ofHours(quantity * 24 * 7)); default: break; } } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(); } }  static Duration getDuration(String time); static TimeInterval getTimeInterval(String time); static TimeInterval getTimeInterval(String start, String end); static Instant getInstant(String time);  }