KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > document > DateField


1 package org.apache.lucene.document;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Date JavaDoc;
20
21 import org.apache.lucene.search.PrefixQuery; // for javadoc
22
import org.apache.lucene.search.RangeQuery; // for javadoc
23

24 /**
25  * Provides support for converting dates to strings and vice-versa.
26  * The strings are structured so that lexicographic sorting orders by date,
27  * which makes them suitable for use as field values and search terms.
28  *
29  * <P>Note that this class saves dates with millisecond granularity,
30  * which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
31  * queries are expanded to a BooleanQuery with a potentially large number
32  * of terms when searching. Thus you might want to use
33  * {@link DateTools} instead.
34  *
35  * <P>
36  * Note: dates before 1970 cannot be used, and therefore cannot be
37  * indexed when using this class. See {@link DateTools} for an
38  * alternative without such a limitation.
39  *
40  * @deprecated If you build a new index, use {@link DateTools} instead. For
41  * existing indices you can continue using this class, as it will not be
42  * removed in the near future despite being deprecated.
43  */

44 public class DateField {
45   
46   private DateField() {}
47
48   // make date strings long enough to last a millenium
49
private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
50                            Character.MAX_RADIX).length();
51
52   public static String JavaDoc MIN_DATE_STRING() {
53     return timeToString(0);
54   }
55
56   public static String JavaDoc MAX_DATE_STRING() {
57     char[] buffer = new char[DATE_LEN];
58     char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
59     for (int i = 0 ; i < DATE_LEN; i++)
60       buffer[i] = c;
61     return new String JavaDoc(buffer);
62   }
63
64   /**
65    * Converts a Date to a string suitable for indexing.
66    * @throws RuntimeException if the date specified in the
67    * method argument is before 1970
68    */

69   public static String JavaDoc dateToString(Date JavaDoc date) {
70     return timeToString(date.getTime());
71   }
72   /**
73    * Converts a millisecond time to a string suitable for indexing.
74    * @throws RuntimeException if the time specified in the
75    * method argument is negative, that is, before 1970
76    */

77   public static String JavaDoc timeToString(long time) {
78     if (time < 0)
79       throw new RuntimeException JavaDoc("time '" + time + "' is too early, must be >= 0");
80
81     String JavaDoc s = Long.toString(time, Character.MAX_RADIX);
82
83     if (s.length() > DATE_LEN)
84       throw new RuntimeException JavaDoc("time '" + time + "' is too late, length of string " +
85           "representation must be <= " + DATE_LEN);
86
87     // Pad with leading zeros
88
if (s.length() < DATE_LEN) {
89       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
90       while (sb.length() < DATE_LEN)
91         sb.insert(0, 0);
92       s = sb.toString();
93     }
94
95     return s;
96   }
97
98   /** Converts a string-encoded date into a millisecond time. */
99   public static long stringToTime(String JavaDoc s) {
100     return Long.parseLong(s, Character.MAX_RADIX);
101   }
102   /** Converts a string-encoded date into a Date object. */
103   public static Date JavaDoc stringToDate(String JavaDoc s) {
104     return new Date JavaDoc(stringToTime(s));
105   }
106 }
107
Popular Tags