@Test public void getDuration() { Duration lastMin = TimeParser.getDuration("last min"); Assert.assertEquals("Failed to get Duration for last min", 60, lastMin.getSeconds()); Duration lastHour = TimeParser.getDuration("last hour"); Assert.assertEquals("Failed to get Duration for last hour", 60 * 60, lastHour.getSeconds()); Duration lastDay = TimeParser.getDuration("last day"); Assert.assertEquals("Failed to get Duration for last day", 60 * 60 * 24, lastDay.getSeconds()); Duration lastWeek = TimeParser.getDuration("last week"); Assert.assertEquals("Failed to get Duration for last week", 60 * 60 * 24 * 7, lastWeek.getSeconds()); Duration last5Min = TimeParser.getDuration("last 5 mins"); Assert.assertEquals("Failed to get Duration for last 5 mins", 60 * 5, last5Min.getSeconds()); Duration last5Hour = TimeParser.getDuration("last 5 hours"); Assert.assertEquals("Failed to get Duration for last 5 hours", 60 * 60 * 5, last5Hour.getSeconds()); Duration last5Day = TimeParser.getDuration("last 5 days"); Assert.assertEquals("Failed to get Duration for last 5 days", 60 * 60 * 24 * 5, last5Day.getSeconds()); Duration last5Week = TimeParser.getDuration("last 5 weeks"); Assert.assertEquals("Failed to get Duration for last 5 weeks", 60 * 60 * 24 * 7 * 5, last5Week.getSeconds()); Duration oneMinAgo = TimeParser.getDuration("1 min ago"); Assert.assertEquals("Failed to get Duration for 1 min ago", 60, oneMinAgo.getSeconds()); Duration oneHourAgo = TimeParser.getDuration("1 hour ago"); Assert.assertEquals("Failed to get Duration for 1 hour ago", 60 * 60, oneHourAgo.getSeconds()); Duration oneDayAgo = TimeParser.getDuration("1 day ago"); Assert.assertEquals("Failed to get Duration for 1 days ago", 60 * 60 * 24, oneDayAgo.getSeconds()); Duration oneWeekAgo = TimeParser.getDuration("1 week ago"); Assert.assertEquals("Failed to get Duration for 1 week ago", 60 * 60 * 24 * 7, oneWeekAgo.getSeconds()); Duration fiveMinsAgo = TimeParser.getDuration("5 mins ago"); Assert.assertEquals("Failed to get Duration for 5 mins ago", 60 * 5, fiveMinsAgo.getSeconds()); Duration fiveHoursAgo = TimeParser.getDuration("5 hours ago"); Assert.assertEquals("Failed to get Duration for 5 hours ago", 60 * 60 * 5, fiveHoursAgo.getSeconds()); Duration fiveDaysAgo = TimeParser.getDuration("5 days ago"); Assert.assertEquals("Failed to get Duration for 5 days ago", 60 * 60 * 24 * 5, fiveDaysAgo.getSeconds()); Duration fiveWeeksAgo = TimeParser.getDuration("5 weeks ago"); Assert.assertEquals("Failed to get Duration for 5 week ago", 60 * 60 * 24 * 7 * 5, fiveWeeksAgo.getSeconds()); Duration last4Min = TimeParser.getDuration("Last 4 Mins"); Assert.assertEquals("Failed to get Duration for Last 4 Mins", 60 * 4, last4Min.getSeconds()); Duration last4Hour = TimeParser.getDuration("Last 4 Hours"); Assert.assertEquals("Failed to get Duration for Last 4 Hours", 60 * 60 * 4, last4Hour.getSeconds()); Duration last4Day = TimeParser.getDuration("Last 4 Day"); Assert.assertEquals("Failed to get Duration for Last 4 Day", 60 * 60 * 24 * 4, last4Day.getSeconds()); Duration last4Week = TimeParser.getDuration("Last 4 WEEKS"); Assert.assertEquals("Failed to get Duration for Last 4 WEEKS", 60 * 60 * 24 * 7 * 4, last4Week.getSeconds()); Duration last3Min = TimeParser.getDuration("last 3 min"); Assert.assertEquals("Failed to get Duration for last 3 min", 60 * 3, last3Min.getSeconds()); Duration last3Hour = TimeParser.getDuration("last 3 hour"); Assert.assertEquals("Failed to get Duration for last 3 hour", 60 * 60 * 3, last3Hour.getSeconds()); Duration last3Day = TimeParser.getDuration("last 3 day"); Assert.assertEquals("Failed to get Duration for last 3 day", 60 * 60 * 24 * 3, last3Day.getSeconds()); Duration last3Week = TimeParser.getDuration("last 3 week"); Assert.assertEquals("Failed to get Duration for last 3 week", 60 * 60 * 24 * 7 * 3, last3Week.getSeconds()); Duration last2Mins = TimeParser.getDuration("last 2mins"); Assert.assertEquals("Failed to get Duration for last 2mins", 60 * 2, last2Mins.getSeconds()); Duration last2Hours = TimeParser.getDuration("last 2hours"); Assert.assertEquals("Failed to get Duration for last 2hours", 60 * 60 * 2, last2Hours.getSeconds()); Duration last2Days = TimeParser.getDuration("last 2days"); Assert.assertEquals("Failed to get Duration for last 2days", 60 * 60 * 24 * 2, last2Days.getSeconds()); Duration last2Weeks = TimeParser.getDuration("last 2weeks"); Assert.assertEquals("Failed to get Duration for last 2weeks", 60 * 60 * 24 * 7 * 2, last2Weeks.getSeconds()); Duration twoMinsAgo = TimeParser.getDuration("2mins ago"); Assert.assertEquals("Failed to get Duration for 2mins ago", 60 * 2, twoMinsAgo.getSeconds()); Duration twoHoursAgo = TimeParser.getDuration("2hours ago"); Assert.assertEquals("Failed to get Duration for 2hours ago", 60 * 60 * 2, twoHoursAgo.getSeconds()); Duration twoDaysAgo = TimeParser.getDuration("2days ago"); Assert.assertEquals("Failed to get Duration for 2days ago", 60 * 60 * 24 * 2, twoDaysAgo.getSeconds()); Duration twoWeeksAgo = TimeParser.getDuration("2weeks ago"); Assert.assertEquals("Failed to get Duration for 2weeks ago", 60 * 60 * 24 * 7 * 2, twoWeeksAgo.getSeconds()); }
public static Duration getDuration(String time) { 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); } 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); } unit = unit.toLowerCase(); switch (unit) { case "min": case "mins": return Duration.ofMinutes(quantity); case "hour": case "hours": return Duration.ofHours(quantity); case "day": case "days": return Duration.ofHours(quantity * 24); case "week": case "weeks": return Duration.ofHours(quantity * 24 * 7); default: break; } return null; }
TimeParser { public static Duration getDuration(String time) { 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); } 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); } unit = unit.toLowerCase(); switch (unit) { case "min": case "mins": return Duration.ofMinutes(quantity); case "hour": case "hours": return Duration.ofHours(quantity); case "day": case "days": return Duration.ofHours(quantity * 24); case "week": case "weeks": return Duration.ofHours(quantity * 24 * 7); default: break; } return null; } }
TimeParser { public static Duration getDuration(String time) { 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); } 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); } unit = unit.toLowerCase(); switch (unit) { case "min": case "mins": return Duration.ofMinutes(quantity); case "hour": case "hours": return Duration.ofHours(quantity); case "day": case "days": return Duration.ofHours(quantity * 24); case "week": case "weeks": return Duration.ofHours(quantity * 24 * 7); default: break; } return null; }  }
TimeParser { public static Duration getDuration(String time) { 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); } 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); } unit = unit.toLowerCase(); switch (unit) { case "min": case "mins": return Duration.ofMinutes(quantity); case "hour": case "hours": return Duration.ofHours(quantity); case "day": case "days": return Duration.ofHours(quantity * 24); case "week": case "weeks": return Duration.ofHours(quantity * 24 * 7); default: break; } return null; }  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 Duration getDuration(String time) { 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); } 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); } unit = unit.toLowerCase(); switch (unit) { case "min": case "mins": return Duration.ofMinutes(quantity); case "hour": case "hours": return Duration.ofHours(quantity); case "day": case "days": return Duration.ofHours(quantity * 24); case "week": case "weeks": return Duration.ofHours(quantity * 24 * 7); default: break; } return null; }  static Duration getDuration(String time); static TimeInterval getTimeInterval(String time); static TimeInterval getTimeInterval(String start, String end); static Instant getInstant(String time);  }