public static String replaceDateValues(String aQuery,DateTime replaceWith){
  Matcher matcher=DATE_REPLACE_PATTERN.matcher(aQuery);
  while (matcher.find()) {
    final String format=matcher.group(1);
    DateTimeFormatter formatter=QueryReplaceUtil.makeDateTimeFormat(format);
    if (matcher.groupCount() > 3 && matcher.group(3) != null && matcher.group(4) != null) {
      int count=Integer.valueOf(matcher.group(3));
      DateMod dm=DateMod.valueOf(matcher.group(4));
      DateTime toMod=new DateTime(replaceWith);
      if (dm.equals(DateMod.H)) {
        toMod=toMod.minusHours(count);
      }
 else       if (dm.equals(DateMod.D)) {
        toMod=toMod.minusDays(count);
      }
 else       if (dm.equals(DateMod.M)) {
        toMod=toMod.minusMonths(count);
      }
      aQuery=aQuery.replace(matcher.group(),formatter.print(toMod));
    }
 else {
      aQuery=aQuery.replace(matcher.group(),formatter.print(replaceWith));
    }
    matcher=DATE_REPLACE_PATTERN.matcher(aQuery);
  }
  return aQuery;
}

public static String replaceDateValues ( String aQuery , DateTime replaceWith ) { Matcher matcher = DATE_REPLACE_PATTERN . matcher ( aQuery ) ; while ( matcher . find ( ) ) { final String format = matcher . group ( 1 ) ; DateTimeFormatter formatter = QueryReplaceUtil . makeDateTimeFormat ( format ) ; if ( matcher . groupCount ( ) > 3 && matcher . group ( 3 ) != null && matcher . group ( 4 ) != null ) { int count = Integer . valueOf ( matcher . group ( 3 ) ) ; DateMod dm = DateMod . valueOf ( matcher . group ( 4 ) ) ; DateTime toMod = new DateTime ( replaceWith ) ; if ( dm . equals ( DateMod . H ) ) { toMod = toMod . minusHours ( count ) ; } else if ( dm . equals ( DateMod . D ) ) { toMod = toMod . minusDays ( count ) ; } else if ( dm . equals ( DateMod . M ) ) { toMod = toMod . minusMonths ( count ) ; } aQuery = aQuery . replace ( matcher . group ( ) , formatter . print ( toMod ) ) ; } else { aQuery = aQuery . replace ( matcher . group ( ) , formatter . print ( replaceWith ) ) ; } matcher = DATE_REPLACE_PATTERN . matcher ( aQuery ) ; } return aQuery ; }
replace date formats with the datetime values passed in . operations like " yyyymmdd - 1d " are supported for doing simple subtraction on the passed in datetime . see datemod for supported modification values .
