public static TimeDiff parseFrom(String diffStr) throws LensException { // Get the relative diff part to get eventual date based on now. Matcher qtyMatcher = P_QUANTITY.matcher(diffStr); int qty = 1; if (qtyMatcher.find()) { qty = Integer.parseInt(qtyMatcher.group()); } Matcher signageMatcher = P_SIGNAGE.matcher(diffStr); if (signageMatcher.find()) { String sign = signageMatcher.group(); if ("-".equals(sign)) { qty = -qty; } } Matcher unitMatcher = P_UNIT.matcher(diffStr); if (unitMatcher.find()) { return new TimeDiff(qty, UpdatePeriod.fromUnitName(unitMatcher.group().toLowerCase())); } return new TimeDiff(0, UpdatePeriod.CONTINUOUS); }
public static TimeDiff parseFrom(String diffStr) throws LensException { // Get the relative diff part to get eventual date based on now. Matcher qtyMatcher = P_QUANTITY.matcher(diffStr); int qty = 1; if (qtyMatcher.find()) { qty = Integer.parseInt(qtyMatcher.group()); } Matcher signageMatcher = P_SIGNAGE.matcher(diffStr); if (signageMatcher.find()) { String sign = signageMatcher.group(); if ("-".equals(sign)) { qty = -qty; } } Matcher unitMatcher = P_UNIT.matcher(diffStr); if (unitMatcher.find()) { return new TimeDiff(qty, UpdatePeriod.fromUnitName(unitMatcher.group().toLowerCase())); } return new TimeDiff(0, UpdatePeriod.CONTINUOUS); }
public static Date resolveRelativeDate(String str, Date now) throws LensException { if (StringUtils.isBlank(str)) { throw new LensException(LensCubeErrorCode.NULL_DATE_VALUE.getLensErrorInfo()); } // Resolve NOW with proper granularity Calendar calendar = Calendar.getInstance(); calendar.setTime(now); str = str.toLowerCase(); Matcher relativeMatcher = P_RELATIVE.matcher(str); if (relativeMatcher.find()) { String nowWithGranularity = relativeMatcher.group(); nowWithGranularity = nowWithGranularity.replaceAll("now", ""); nowWithGranularity = nowWithGranularity.replaceAll("\\.", ""); Matcher granularityMatcher = P_UNIT.matcher(nowWithGranularity); if (granularityMatcher.find()) { calendar = UpdatePeriod.fromUnitName(granularityMatcher.group().toLowerCase()).truncate(calendar); } } // Get rid of 'now' part and whitespace String diffStr = str.replaceAll(RELATIVE, "").replace(WSPACE, ""); TimeDiff diff = TimeDiff.parseFrom(diffStr); return diff.offsetFrom(calendar.getTime()); }
public static Date resolveRelativeDate(String str, Date now) throws LensException { if (StringUtils.isBlank(str)) { throw new LensException(LensCubeErrorCode.NULL_DATE_VALUE.getLensErrorInfo()); } // Resolve NOW with proper granularity Calendar calendar = Calendar.getInstance(); calendar.setTime(now); str = str.toLowerCase(); Matcher relativeMatcher = P_RELATIVE.matcher(str); if (relativeMatcher.find()) { String nowWithGranularity = relativeMatcher.group(); nowWithGranularity = nowWithGranularity.replaceAll("now", ""); nowWithGranularity = nowWithGranularity.replaceAll("\\.", ""); Matcher granularityMatcher = P_UNIT.matcher(nowWithGranularity); if (granularityMatcher.find()) { calendar = UpdatePeriod.fromUnitName(granularityMatcher.group().toLowerCase()).truncate(calendar); } } // Get rid of 'now' part and whitespace String diffStr = str.replaceAll(RELATIVE, "").replace(WSPACE, ""); TimeDiff diff = TimeDiff.parseFrom(diffStr); return diff.offsetFrom(calendar.getTime()); }