KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > wrapper > lucene > DateField


1 package org.enhydra.snapper.wrapper.lucene;
2
3 /**
4
5  */

6
7 import java.util.Date JavaDoc;
8
9 /**
10  * Provides support for converting dates to strings and vice-versa.
11  * The strings are structured so that lexicographic sorting orders by date,
12  * which makes them suitable for use as field values and search terms.
13  *
14  * <P>
15  * Note that you do not have to use this class, you can just save your
16  * dates as strings if lexicographic sorting orders them by date. This is
17  * the case for example for dates like <code>yyyy-mm-dd hh:mm:ss</code>
18  * (of course you can leave out the delimiter characters to save some space).
19  * The advantage with using such a format is that you can easily save dates
20  * with the required granularity, e.g. leaving out seconds. This saves memory
21  * when searching with a RangeQuery or PrefixQuery, as Lucene
22  * expands these queries to a BooleanQuery with potentially very many terms.
23  *
24  * <P>
25  * Note: dates before 1970 cannot be used, and therefore cannot be
26  * indexed when using this class.
27  */

28 public class DateField {
29   private DateField() {}
30
31   // make date strings long enough to last a millenium
32
private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
33                            Character.MAX_RADIX).length();
34
35   public static String JavaDoc MIN_DATE_STRING() {
36     return timeToString(0);
37   }
38
39   public static String JavaDoc MAX_DATE_STRING() {
40     char[] buffer = new char[DATE_LEN];
41     char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
42     for (int i = 0 ; i < DATE_LEN; i++)
43       buffer[i] = c;
44     return new String JavaDoc(buffer);
45   }
46
47   /**
48    * Converts a Date to a string suitable for indexing.
49    * @throws RuntimeException if the date specified in the
50    * method argument is before 1970
51    */

52   public static String JavaDoc dateToString(Date JavaDoc date) {
53     return timeToString(date.getTime());
54   }
55   /**
56    * Converts a millisecond time to a string suitable for indexing.
57    * @throws RuntimeException if the time specified in the
58    * method argument is negative, that is, before 1970
59    */

60   public static String JavaDoc timeToString(long time) {
61     if (time < 0)
62       throw new RuntimeException JavaDoc("time too early");
63
64     String JavaDoc s = Long.toString(time, Character.MAX_RADIX);
65
66     if (s.length() > DATE_LEN)
67       throw new RuntimeException JavaDoc("time too late");
68
69     // Pad with leading zeros
70
if (s.length() < DATE_LEN) {
71       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
72       while (sb.length() < DATE_LEN)
73         sb.insert(0, 0);
74       s = sb.toString();
75     }
76
77     return s;
78   }
79
80   /** Converts a string-encoded date into a millisecond time. */
81   public static long stringToTime(String JavaDoc s) {
82     return Long.parseLong(s, Character.MAX_RADIX);
83   }
84   /** Converts a string-encoded date into a Date object. */
85   public static Date JavaDoc stringToDate(String JavaDoc s) {
86     return new Date JavaDoc(stringToTime(s));
87   }
88 }
89
90
Popular Tags