@Test public void testParseThreeWeeksHence() { PointInTime expected3WeeksHence = PointInTime.today().addWeeks(3); PointInTime threeWeeksHence = RelativeDateTimeFormat.parse("TODAY+3w"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); threeWeeksHence = RelativeDateTimeFormat.parse("Today+3W"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); threeWeeksHence = RelativeDateTimeFormat.parse("T+3W"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); threeWeeksHence = RelativeDateTimeFormat.parse("t+3W"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); threeWeeksHence = RelativeDateTimeFormat.parse("t+3w"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); threeWeeksHence = RelativeDateTimeFormat.parse("+3w"); assertThat(threeWeeksHence, is(expected3WeeksHence)); assertThat(threeWeeksHence.getPrecision(), is(Precision.DATE)); }
public static PointInTime parse(String text) { if (text == null) return null; if (text.length() == 0) return null; Matcher m = RELATIVE_DATETIME_PATTERN.matcher(text); if (!m.matches()) { throw new IllegalArgumentException("'" + text + "' is not a recognizable relative date/time pattern"); } String todayOrNowOrNoonOrMidnight = m.group(1); PointInTime t = null; if (todayOrNowOrNoonOrMidnight == null || todayOrNowOrNoonOrMidnight.startsWith("T") || todayOrNowOrNoonOrMidnight.startsWith("t")) { t = PointInTime.today(); } else if (todayOrNowOrNoonOrMidnight.startsWith("N") || todayOrNowOrNoonOrMidnight.startsWith("n")) { if (todayOrNowOrNoonOrMidnight.endsWith("N") || todayOrNowOrNoonOrMidnight.endsWith("n")) { t = PointInTime.now().toPointInTimeAtNoon(); } else { t = PointInTime.now(); } } else if (todayOrNowOrNoonOrMidnight.equalsIgnoreCase("MID")) { t = PointInTime.now().toPointInTimeAtMidnight(); } if (t == null) throw new IllegalArgumentException(); String operator = m.group(4); String periodStr = m.group(5); Period period = hasLength(periodStr) ? getPeriodFormatter().parsePeriod(periodStr) : null; if (hasLength(operator) && period != null) { if (operator.equals(MINUS)) { t = t.subtract(period); } else if (operator.equals(PLUS)) { t = t.add(period); } } return t; }
RelativeDateTimeFormat { public static PointInTime parse(String text) { if (text == null) return null; if (text.length() == 0) return null; Matcher m = RELATIVE_DATETIME_PATTERN.matcher(text); if (!m.matches()) { throw new IllegalArgumentException("'" + text + "' is not a recognizable relative date/time pattern"); } String todayOrNowOrNoonOrMidnight = m.group(1); PointInTime t = null; if (todayOrNowOrNoonOrMidnight == null || todayOrNowOrNoonOrMidnight.startsWith("T") || todayOrNowOrNoonOrMidnight.startsWith("t")) { t = PointInTime.today(); } else if (todayOrNowOrNoonOrMidnight.startsWith("N") || todayOrNowOrNoonOrMidnight.startsWith("n")) { if (todayOrNowOrNoonOrMidnight.endsWith("N") || todayOrNowOrNoonOrMidnight.endsWith("n")) { t = PointInTime.now().toPointInTimeAtNoon(); } else { t = PointInTime.now(); } } else if (todayOrNowOrNoonOrMidnight.equalsIgnoreCase("MID")) { t = PointInTime.now().toPointInTimeAtMidnight(); } if (t == null) throw new IllegalArgumentException(); String operator = m.group(4); String periodStr = m.group(5); Period period = hasLength(periodStr) ? getPeriodFormatter().parsePeriod(periodStr) : null; if (hasLength(operator) && period != null) { if (operator.equals(MINUS)) { t = t.subtract(period); } else if (operator.equals(PLUS)) { t = t.add(period); } } return t; } }
RelativeDateTimeFormat { public static PointInTime parse(String text) { if (text == null) return null; if (text.length() == 0) return null; Matcher m = RELATIVE_DATETIME_PATTERN.matcher(text); if (!m.matches()) { throw new IllegalArgumentException("'" + text + "' is not a recognizable relative date/time pattern"); } String todayOrNowOrNoonOrMidnight = m.group(1); PointInTime t = null; if (todayOrNowOrNoonOrMidnight == null || todayOrNowOrNoonOrMidnight.startsWith("T") || todayOrNowOrNoonOrMidnight.startsWith("t")) { t = PointInTime.today(); } else if (todayOrNowOrNoonOrMidnight.startsWith("N") || todayOrNowOrNoonOrMidnight.startsWith("n")) { if (todayOrNowOrNoonOrMidnight.endsWith("N") || todayOrNowOrNoonOrMidnight.endsWith("n")) { t = PointInTime.now().toPointInTimeAtNoon(); } else { t = PointInTime.now(); } } else if (todayOrNowOrNoonOrMidnight.equalsIgnoreCase("MID")) { t = PointInTime.now().toPointInTimeAtMidnight(); } if (t == null) throw new IllegalArgumentException(); String operator = m.group(4); String periodStr = m.group(5); Period period = hasLength(periodStr) ? getPeriodFormatter().parsePeriod(periodStr) : null; if (hasLength(operator) && period != null) { if (operator.equals(MINUS)) { t = t.subtract(period); } else if (operator.equals(PLUS)) { t = t.add(period); } } return t; }  }
RelativeDateTimeFormat { public static PointInTime parse(String text) { if (text == null) return null; if (text.length() == 0) return null; Matcher m = RELATIVE_DATETIME_PATTERN.matcher(text); if (!m.matches()) { throw new IllegalArgumentException("'" + text + "' is not a recognizable relative date/time pattern"); } String todayOrNowOrNoonOrMidnight = m.group(1); PointInTime t = null; if (todayOrNowOrNoonOrMidnight == null || todayOrNowOrNoonOrMidnight.startsWith("T") || todayOrNowOrNoonOrMidnight.startsWith("t")) { t = PointInTime.today(); } else if (todayOrNowOrNoonOrMidnight.startsWith("N") || todayOrNowOrNoonOrMidnight.startsWith("n")) { if (todayOrNowOrNoonOrMidnight.endsWith("N") || todayOrNowOrNoonOrMidnight.endsWith("n")) { t = PointInTime.now().toPointInTimeAtNoon(); } else { t = PointInTime.now(); } } else if (todayOrNowOrNoonOrMidnight.equalsIgnoreCase("MID")) { t = PointInTime.now().toPointInTimeAtMidnight(); } if (t == null) throw new IllegalArgumentException(); String operator = m.group(4); String periodStr = m.group(5); Period period = hasLength(periodStr) ? getPeriodFormatter().parsePeriod(periodStr) : null; if (hasLength(operator) && period != null) { if (operator.equals(MINUS)) { t = t.subtract(period); } else if (operator.equals(PLUS)) { t = t.add(period); } } return t; }  static PeriodFormatter getPeriodFormatter(); static PointInTime parse(String text); }
RelativeDateTimeFormat { public static PointInTime parse(String text) { if (text == null) return null; if (text.length() == 0) return null; Matcher m = RELATIVE_DATETIME_PATTERN.matcher(text); if (!m.matches()) { throw new IllegalArgumentException("'" + text + "' is not a recognizable relative date/time pattern"); } String todayOrNowOrNoonOrMidnight = m.group(1); PointInTime t = null; if (todayOrNowOrNoonOrMidnight == null || todayOrNowOrNoonOrMidnight.startsWith("T") || todayOrNowOrNoonOrMidnight.startsWith("t")) { t = PointInTime.today(); } else if (todayOrNowOrNoonOrMidnight.startsWith("N") || todayOrNowOrNoonOrMidnight.startsWith("n")) { if (todayOrNowOrNoonOrMidnight.endsWith("N") || todayOrNowOrNoonOrMidnight.endsWith("n")) { t = PointInTime.now().toPointInTimeAtNoon(); } else { t = PointInTime.now(); } } else if (todayOrNowOrNoonOrMidnight.equalsIgnoreCase("MID")) { t = PointInTime.now().toPointInTimeAtMidnight(); } if (t == null) throw new IllegalArgumentException(); String operator = m.group(4); String periodStr = m.group(5); Period period = hasLength(periodStr) ? getPeriodFormatter().parsePeriod(periodStr) : null; if (hasLength(operator) && period != null) { if (operator.equals(MINUS)) { t = t.subtract(period); } else if (operator.equals(PLUS)) { t = t.add(period); } } return t; }  static PeriodFormatter getPeriodFormatter(); static PointInTime parse(String text); static final Pattern TODAY_PATTERN; static final Pattern NOW_PATTERN; static final Pattern MIDNIGHT_PATTERN; static final Pattern NOON_PATTERN; static final Pattern RELATIVE_DATETIME_PATTERN; static final String MINUS; static final String PLUS; }