List of utility methods to do Regex Time Validate
long | parseTime(String s) parse Time Pattern p = Pattern.compile("(\\d+)([smhd])"); Matcher m = p.matcher(s); long num = 0; while (m.find()) { int i = Integer.parseInt(m.group(1)); String unit = m.group(2); if (unit.equals("m")) i = i * 60; ... |
long | parseTime(String str) convert time format into milliseconds long sum = 0; Matcher matcher = pattern.matcher(str); while (matcher.find()) { switch (matcher.group(2)) { case "h": sum += Long.parseLong(matcher.group(1)) * 60 * 60 * 1000; break; case "m": ... |
long | parseTime(String t) parse Time try { if (!t.matches("[0-9]+d[a-z]*")) return Math.round(Double.parseDouble(t) * 60) * 1000; else { Pattern dayPattern = Pattern.compile("([0-9]+)d[a-z]*", Pattern.CASE_INSENSITIVE); Matcher m = dayPattern.matcher(t); if (m.matches()) { return Integer.parseInt(m.group(1)) * 24 * 60 * 60 * 1000; ... |
long | parseTime(String value) Parse value of TM property. value = value.trim(); try { return (long) (Double.parseDouble(value) * 1000); } catch (NumberFormatException e1) { try { Pattern pattern; Matcher matcher; ... |
Calendar | parseTimeDifference(String input) Create a calendar object for the current time plus the time specified by the input string. Pattern pattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE); int[] units = new int[] { 0, 0, 0, 0, 0, 0, 0 }; boolean match = false; Matcher matcher = pattern.matcher(input); while (matcher.find()) { ... |
Long | parseTimeOffset(String s) Parses time offset from string (Examples: "-1d", "+1mon") Matcher matcher = timeOffsetPattern.matcher(s.replaceAll("^['\"]|['\"]$", "")); if (!matcher.matches()) return 0L; int sign = matcher.group(1).equals("+") ? 1 : -1; long offset = Integer.parseInt(matcher.group(2)) * getUnitValue(matcher.group(3)); return offset * sign; |
long | parseTimePeriod(String period) Parses a string of type "1y2mo3w4d5h6m7s", where the units represent years, months, weeks, days, hours, minutes and second respectively. Pattern relativeDatePattern = Pattern.compile("(\\d+(?:[.,]\\d+)?)(mo|[smhdwy])"); Matcher relativeDateMatcher = relativeDatePattern.matcher(period); relativeDateMatcher.reset(); long periodSeconds = 0; while (relativeDateMatcher.find()) { double time = Double.parseDouble(relativeDateMatcher.group(1)); String unitStr = relativeDateMatcher.group(2).toLowerCase(); int unitMultiplier = 0; ... |
long | parseTimePeriod(String s) Note, right now this just parses the hour/minute/second periods if (!s.startsWith("P")) { throw new IllegalArgumentException("Unknown time period:" + s); s = s.substring(1); String tmp = "((\\d+)Y)?((\\d+)M)?((\\d+)D)?(T((\\d+)H)?((\\d+)M)?((\\d+)S)?)"; Pattern pattern = Pattern.compile(tmp); Matcher matcher = pattern.matcher(s); boolean ok = matcher.find(); ... |
long | parseTimeSpan(String timeSpan) Parse a time span that's in minutes by defualt. Pattern hourRegex = Pattern.compile("(\\d+)h"); Pattern minRegex = Pattern.compile("(\\d+)m"); Pattern secondRegex = Pattern.compile("(\\d+)s"); Matcher hourMatch = hourRegex.matcher(timeSpan); Matcher minMatch = minRegex.matcher(timeSpan); Matcher secMatch = secondRegex.matcher(timeSpan); boolean hourMatched = hourMatch.find(); boolean minMatched = minMatch.find(); ... |
Date | parseTimestamp(final String value) Parses timestamp value from string. final Matcher matcher = PATTERN_TIMESTAMP.matcher(value); if (!matcher.matches()) { throw new RuntimeException("Invalid timestamp value: " + value); final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.clear(); calendar.set(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)) - 1, Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)), ... |