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

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

Introduction

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

Prototype

public DateMathParser() 

Source Link

Document

Chooses defaults based on the current request.

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./*www . j  a  va 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.//from   ww  w  . j  av a2s  .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 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:de.qaware.chronix.solr.query.date.DateQueryParser.java

License:Apache License

/**
 * @return the end date for the range query
 * @throws ParseException//w  ww.j av a  2s  .c o  m
 */
private long parseDate(String dateQueryTerm) throws ParseException {
    return new DateMathParser().parseMath(dateQueryTerm).getTime();
}

From source file:de.qaware.chronix.solr.retention.ChronixRetentionHandler.java

License:Apache License

/**
 * @return the end date for the range query
 * @throws ParseException/*from w ww . j  a  v a2 s  .  com*/
 */
private Date parseEndDate() throws ParseException {
    return new DateMathParser().parseMath(String.format("+0MILLISECOND-%s", timeSeriesAge));
}

From source file:org.opencommercesearch.Utils.java

License:Apache License

private static int daysBetween(String from, String to) throws ParseException {
    DateMathParser dmp = new DateMathParser();
    Date fromDate = parseDate(from, dmp);
    Date toDate = parseDate(to, dmp);
    return Days.daysBetween(new DateTime(fromDate), new DateTime(toDate)).getDays();
}