FileDocCategorySizeDatePackage
DateField.javaAPI DocApache Lucene 1.93652Mon Feb 27 11:12:36 GMT 2006org.apache.lucene.document

DateField

public class DateField extends Object
Provides support for converting dates to strings and vice-versa. The strings are structured so that lexicographic sorting orders by date, which makes them suitable for use as field values and search terms.

Note that this class saves dates with millisecond granularity, which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those queries are expanded to a BooleanQuery with a potentially large number of terms when searching. Thus you might want to use {@link DateTools} instead.

Note: dates before 1970 cannot be used, and therefore cannot be indexed when using this class. See {@link DateTools} for an alternative without such a limitation.

deprecated
If you build a new index, use {@link DateTools} instead. For existing indices you can continue using this class, as it will not be removed in the near future despite being deprecated.

Fields Summary
private static int
DATE_LEN
Constructors Summary
private DateField()

Methods Summary
public static java.lang.StringMAX_DATE_STRING()

    char[] buffer = new char[DATE_LEN];
    char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
    for (int i = 0 ; i < DATE_LEN; i++)
      buffer[i] = c;
    return new String(buffer);
  
public static java.lang.StringMIN_DATE_STRING()


      
    return timeToString(0);
  
public static java.lang.StringdateToString(java.util.Date date)
Converts a Date to a string suitable for indexing.

throws
RuntimeException if the date specified in the method argument is before 1970

    return timeToString(date.getTime());
  
public static java.util.DatestringToDate(java.lang.String s)
Converts a string-encoded date into a Date object.

    return new Date(stringToTime(s));
  
public static longstringToTime(java.lang.String s)
Converts a string-encoded date into a millisecond time.

    return Long.parseLong(s, Character.MAX_RADIX);
  
public static java.lang.StringtimeToString(long time)
Converts a millisecond time to a string suitable for indexing.

throws
RuntimeException if the time specified in the method argument is negative, that is, before 1970

    if (time < 0)
      throw new RuntimeException("time '" + time + "' is too early, must be >= 0");

    String s = Long.toString(time, Character.MAX_RADIX);

    if (s.length() > DATE_LEN)
      throw new RuntimeException("time '" + time + "' is too late, length of string " +
          "representation must be <= " + DATE_LEN);

    // Pad with leading zeros
    if (s.length() < DATE_LEN) {
      StringBuffer sb = new StringBuffer(s);
      while (sb.length() < DATE_LEN)
        sb.insert(0, 0);
      s = sb.toString();
    }

    return s;