/** * Returns a formatter that can be use by the current thread if needed to * convert Date objects to the Internal representation. * @throws ParseException * @throws DateParseException * * @deprecated Use {@link org.apache.solr.common.util.DateUtil#parseDate(String)} */ public static Date parseDate( String d ) throws ParseException, DateParseException { return DateUtil.parseDate(d); }
public static Date parseDate(String d, Collection<String> fmts) throws ParseException { // 2007-04-26T08:05:04Z if (d.endsWith("Z") && d.length() > 20) { return getThreadLocalDateFormat().parse(d); } return parseDate(d, fmts, null); }
/** * Returns a formatter that can be use by the current thread if needed to * convert Date objects to the Internal representation. * * @deprecated use {@link org.apache.solr.common.util.DateUtil#getThreadLocalDateFormat()} */ public static DateFormat getThreadLocalDateFormat() { return DateUtil.getThreadLocalDateFormat(); }
/** * Add a numeric range facet. * * @param field The field * @param start The start of range * @param end The end of the range * @param gap The gap between each count * @return this */ public SolrQuery addDateRangeFacet(String field, Date start, Date end, String gap) { add(FacetParams.FACET_RANGE, field); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_START), DateUtil.getThreadLocalDateFormat().format(start)); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_END), DateUtil.getThreadLocalDateFormat().format(end)); add(String.format(Locale.ROOT, "f.%s.%s", field, FacetParams.FACET_RANGE_GAP), gap); this.set(FacetParams.FACET, true); return this; }
/** * Returns a formatter that can be use by the current thread if needed to * convert Date objects to the Internal representation. * * @param d The input date to parse * @return The parsed {@link java.util.Date} * @throws java.text.ParseException If the input can't be parsed */ public static Date parseDate(String d) throws ParseException { return parseDate(d, DEFAULT_DATE_FORMATS); }
/** * Can be used to transform input values based on their {@link org.apache.solr.schema.SchemaField} * <p/> * This implementation only formats dates using the {@link org.apache.solr.common.util.DateUtil}. * * @param val The value to transform * @param schFld The {@link org.apache.solr.schema.SchemaField} * @return The potentially new value. */ protected String transformValue(String val, SchemaField schFld) { String result = val; if (schFld != null && schFld.getType() instanceof DateField) { //try to transform the date try { Date date = DateUtil.parseDate(val, dateFormats); DateFormat df = DateUtil.getThreadLocalDateFormat(); result = df.format(date); } catch (Exception e) { // Let the specific fieldType handle errors // throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid value: " + val + " for field: " + schFld, e); } } return result; }
private static void writeVal(Writer writer, float boost, String name, Object v, String update) throws IOException { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); } else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0, bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if (update == null) { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } } else { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost, "update", update); } else { if (v == null) { XML.writeXML(writer, "field", null, "name", name, "update", update, "null", true); } else { XML.writeXML(writer, "field", v.toString(), "name", name, "update", update); } } } }
@Override public Date read( String txt ) { try { return DateUtil.parseDate(txt); } catch( Exception ex ) { ex.printStackTrace(); } return null; } },
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException { writer.write("<doc boost=\""+doc.getDocumentBoost()+"\">"); for( SolrInputField field : doc ) { float boost = field.getBoost(); String name = field.getName(); for( Object v : field ) { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); }else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0,bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost ); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } // only write the boost for the first multi-valued field // otherwise, the used boost is the product of all the boost values boost = 1.0f; } } writer.write("</doc>"); }
/** Parse a date string in the standard format, or any supported by DateUtil.parseDate */ public Date parseDateLenient(String s, SolrQueryRequest req) throws ParseException { // request could define timezone in the future try { return fmtThreadLocal.get().parse(s); } catch (Exception e) { return DateUtil.parseDate(s); } }
private static DcsEvent getEvent(SolrDocument doc) { DcsEvent event = new DcsEvent(); event.setId(getFirst(doc, EntityField.ID)); Date date = (Date) doc.getFirstValue(EventField.DATE.solrName()); if (date != null) { event.setDate(DateUtil.getThreadLocalDateFormat().format(date)); } event.setDetail(getFirst(doc, EventField.DETAIL)); event.setOutcome(getFirst(doc, EventField.OUTCOME)); event.setEventType(getFirst(doc, EventField.TYPE)); event.setTargets(getEntityRefSet(doc, EventField.TARGET)); event.setAlternateIds(getResourceIdentifierSet(doc)); return event; }
/** * @see org.opencms.search.I_CmsSearchDocument#getFieldValueAsDate(java.lang.String) */ public Date getFieldValueAsDate(String fieldName) { Object o = m_doc.getFieldValue(fieldName); if (o instanceof Date) { return (Date)o; } if (o != null) { try { return DateUtil.parseDate(o.toString()); } catch (ParseException e) { // ignore: not a valid date format LOG.debug(e); } } return null; }
Header lastUpdateDate = response.getFirstHeader(LAST_MODIFIED_HEADER); if (lastUpdateDate != null) { return DateUtil.parseDate(lastUpdateDate.getValue()).getTime();