pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / document / DateField.java
1 package org.apache.lucene.document;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.search.PrefixQuery;
21 import org.apache.lucene.search.TermRangeQuery;
22 import org.apache.lucene.search.NumericRangeQuery; // for javadocs
23 import org.apache.lucene.util.NumericUtils; // for javadocs
24
25 import java.util.Date;   // for javadoc
26 import java.util.Calendar;   // for javadoc
27
28 // do not remove in 3.0, needed for reading old indexes!
29
30 /**
31  * Provides support for converting dates to strings and vice-versa.
32  * The strings are structured so that lexicographic sorting orders by date,
33  * which makes them suitable for use as field values and search terms.
34  *
35  * <P>Note that this class saves dates with millisecond granularity,
36  * which is bad for {@link TermRangeQuery} and {@link PrefixQuery}, as those
37  * queries are expanded to a BooleanQuery with a potentially large number
38  * of terms when searching. Thus you might want to use
39  * {@link DateTools} instead.
40  *
41  * <P>
42  * Note: dates before 1970 cannot be used, and therefore cannot be
43  * indexed when using this class. See {@link DateTools} for an
44  * alternative without such a limitation.
45  *
46  * <P>
47  * Another approach is {@link NumericUtils}, which provides
48  * a sortable binary representation (prefix encoded) of numeric values, which
49  * date/time are.
50  * For indexing a {@link Date} or {@link Calendar}, just get the unix timestamp as
51  * <code>long</code> using {@link Date#getTime} or {@link Calendar#getTimeInMillis} and
52  * index this as a numeric value with {@link NumericField}
53  * and use {@link NumericRangeQuery} to query it.
54  *
55  * @deprecated If you build a new index, use {@link DateTools} or 
56  * {@link NumericField} instead.
57  * This class is included for use with existing
58  * indices and will be removed in a future release (possibly Lucene 4.0).
59  */
60 @Deprecated
61 public class DateField {
62   
63   private DateField() {}
64
65   // make date strings long enough to last a millenium
66   private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
67                                                Character.MAX_RADIX).length();
68
69   public static String MIN_DATE_STRING() {
70     return timeToString(0);
71   }
72
73   public static String MAX_DATE_STRING() {
74     char[] buffer = new char[DATE_LEN];
75     char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
76     for (int i = 0 ; i < DATE_LEN; i++)
77       buffer[i] = c;
78     return new String(buffer);
79   }
80
81   /**
82    * Converts a Date to a string suitable for indexing.
83    * @throws RuntimeException if the date specified in the
84    * method argument is before 1970
85    */
86   public static String dateToString(Date date) {
87     return timeToString(date.getTime());
88   }
89   /**
90    * Converts a millisecond time to a string suitable for indexing.
91    * @throws RuntimeException if the time specified in the
92    * method argument is negative, that is, before 1970
93    */
94   public static String timeToString(long time) {
95     if (time < 0)
96       throw new RuntimeException("time '" + time + "' is too early, must be >= 0");
97
98     String s = Long.toString(time, Character.MAX_RADIX);
99
100     if (s.length() > DATE_LEN)
101       throw new RuntimeException("time '" + time + "' is too late, length of string " +
102           "representation must be <= " + DATE_LEN);
103
104     // Pad with leading zeros
105     if (s.length() < DATE_LEN) {
106       StringBuilder sb = new StringBuilder(s);
107       while (sb.length() < DATE_LEN)
108         sb.insert(0, 0);
109       s = sb.toString();
110     }
111
112     return s;
113   }
114
115   /** Converts a string-encoded date into a millisecond time. */
116   public static long stringToTime(String s) {
117     return Long.parseLong(s, Character.MAX_RADIX);
118   }
119   /** Converts a string-encoded date into a Date object. */
120   public static Date stringToDate(String s) {
121     return new Date(stringToTime(s));
122   }
123 }