コード例 #1
0
  /**
   * Parses a String which may be a date followed by an optional math expression.
   *
   * @param now an optional fixed date to use as "NOW" in the DateMathParser
   * @param val the string to parse
   */
  public Date parseMathLenient(Date now, String val, SolrQueryRequest req) {
    String math = null;
    final DateMathParser p = new DateMathParser();

    if (null != now) p.setNow(now);

    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz + 1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDateLenient(val.substring(0, zz + 1), req));
        } catch (ParseException e) {
          throw new SolrException(
              SolrException.ErrorCode.BAD_REQUEST,
              "Invalid Date in Date Math String:'" + val + '\'',
              e);
        }
      } else {
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Invalid Date String:'" + val + '\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }

    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException(
          SolrException.ErrorCode.BAD_REQUEST, "Invalid Date Math String:'" + val + '\'', e);
    }
  }
コード例 #2
0
 @Override
 public Date parseAndAddGap(Date value, String gap) throws java.text.ParseException {
   final DateMathParser dmp = new DateMathParser();
   dmp.setNow(value);
   return dmp.parseMath(gap);
 }