Example usage for org.apache.solr.util DateMathParser getNow

List of usage examples for org.apache.solr.util DateMathParser getNow

Introduction

In this page you can find the example usage for org.apache.solr.util DateMathParser getNow.

Prototype

public Date getNow() 

Source Link

Document

Returns a clone of this instance's concept of "now" (never null).

Usage

From source file:com.appleframework.solr.util.DateFormatUtil.java

License:Apache License

/**
 * Parses a String which may be a date (in the standard format) followed by
 * an optional math expression./* w  ww  .ja v  a 2 s. c o  m*/
 * 
 * @param now
 *            an optional fixed date to use as "NOW" in the DateMathParser
 * @param val
 *            the string to parse
 */
public static Date parseMath(Date now, String val) {
    String math;
    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(parseDate(val.substring(0, zz + 1)));
            } 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);
    }
}

From source file:com.appleframework.solr.util.DateFormatUtil.java

License:Apache License

/**
 * Parses a String which may be a date followed by an optional math
 * expression.// w w w . jav  a2  s.  c om
 * 
 * @param now
 *            an optional fixed date to use as "NOW" in the DateMathParser
 * @param val
 *            the string to parse
 */
public static Date parseMathLenient(Date now, String val, SolrQueryRequest req) {
    String math;
    final DateMathParser p = new DateMathParser();

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

    if (val.startsWith(DateFormatUtil.NOW)) {
        math = val.substring(DateFormatUtil.NOW.length());
    } else {
        final int zz = val.indexOf(DateFormatUtil.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);
    }
}

From source file:fi.nationallibrary.ndl.solr.schema.NegativeSupportingDateField.java

License:Apache License

/**
 * Needs to be overridden because this method calls parseDate
 * which is a static method in DateField
 *//*from w w  w.  j av a 2  s .  c  o m*/
@Override
public Date parseMath(Date now, String val) {
    // Ugly hax
    if (val.length() == 8)
        val = "AD" + val.substring(0, 4) + "-" + val.substring(4, 6) + "-" + val.substring(6, 8);
    if (val.length() == 12)
        val += "T00:00:00Z";

    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);

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

    if (val.startsWith(NOW)) {
        math = val.substring(NOW.length());
    } else {
        try {
            p.setNow(eraAwareParseDate(val));
        } catch (ParseException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "Invalid Date in Date Math String:'" + val + '\'', e);
        }
    }

    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);
    }
}