KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Calendar JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.TimeZone JavaDoc;
24
25 /**
26  * Provides support for converting dates to strings and vice-versa.
27  * The strings are structured so that lexicographic sorting orders
28  * them by date, which makes them suitable for use as field values
29  * and search terms.
30  *
31  * <P>This class also helps you to limit the resolution of your dates. Do not
32  * save dates with a finer resolution than you really need, as then
33  * RangeQuery and PrefixQuery will require more memory and become slower.
34  *
35  * <P>Compared to {@link DateField} the strings generated by the methods
36  * in this class take slightly more space, unless your selected resolution
37  * is set to <code>Resolution.DAY</code> or lower.
38  */

39 public class DateTools {
40   
41   private final static TimeZone JavaDoc GMT = TimeZone.getTimeZone("GMT");
42
43   private DateTools() {}
44
45   /**
46    * Converts a Date to a string suitable for indexing.
47    *
48    * @param date the date to be converted
49    * @param resolution the desired resolution, see
50    * {@link #round(Date, DateTools.Resolution)}
51    * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
52    * depeding on <code>resolution</code>; using UTC as timezone
53    */

54   public static String JavaDoc dateToString(Date JavaDoc date, Resolution resolution) {
55     return timeToString(date.getTime(), resolution);
56   }
57   
58   /**
59    * Converts a millisecond time to a string suitable for indexing.
60    *
61    * @param time the date expressed as milliseconds since January 1, 1970, 00:00:00 GMT
62    * @param resolution the desired resolution, see
63    * {@link #round(long, DateTools.Resolution)}
64    * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter,
65    * depeding on <code>resolution</code>; using UTC as timezone
66    */

67   public static String JavaDoc timeToString(long time, Resolution resolution) {
68     Calendar JavaDoc cal = Calendar.getInstance(GMT);
69
70     //protected in JDK's prior to 1.4
71
//cal.setTimeInMillis(round(time, resolution));
72

73     cal.setTime(new Date JavaDoc(round(time, resolution)));
74
75     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc();
76     sdf.setTimeZone(GMT);
77     String JavaDoc pattern = null;
78     if (resolution == Resolution.YEAR) {
79       pattern = "yyyy";
80     } else if (resolution == Resolution.MONTH) {
81       pattern = "yyyyMM";
82     } else if (resolution == Resolution.DAY) {
83       pattern = "yyyyMMdd";
84     } else if (resolution == Resolution.HOUR) {
85       pattern = "yyyyMMddHH";
86     } else if (resolution == Resolution.MINUTE) {
87       pattern = "yyyyMMddHHmm";
88     } else if (resolution == Resolution.SECOND) {
89       pattern = "yyyyMMddHHmmss";
90     } else if (resolution == Resolution.MILLISECOND) {
91       pattern = "yyyyMMddHHmmssSSS";
92     } else {
93       throw new IllegalArgumentException JavaDoc("unknown resolution " + resolution);
94     }
95     sdf.applyPattern(pattern);
96     return sdf.format(cal.getTime());
97   }
98
99   /**
100    * Converts a string produced by <code>timeToString</code> or
101    * <code>dateToString</code> back to a time, represented as the
102    * number of milliseconds since January 1, 1970, 00:00:00 GMT.
103    *
104    * @param dateString the date string to be converted
105    * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
106    * @throws ParseException if <code>dateString</code> is not in the
107    * expected format
108    */

109   public static long stringToTime(String JavaDoc dateString) throws ParseException JavaDoc {
110     return stringToDate(dateString).getTime();
111   }
112
113   /**
114    * Converts a string produced by <code>timeToString</code> or
115    * <code>dateToString</code> back to a time, represented as a
116    * Date object.
117    *
118    * @param dateString the date string to be converted
119    * @return the parsed time as a Date object
120    * @throws ParseException if <code>dateString</code> is not in the
121    * expected format
122    */

123   public static Date JavaDoc stringToDate(String JavaDoc dateString) throws ParseException JavaDoc {
124     String JavaDoc pattern = null;
125     if (dateString.length() == 4 )
126       pattern = "yyyy";
127     else if (dateString.length() == 6 )
128       pattern = "yyyyMM";
129     else if (dateString.length() == 8 )
130       pattern = "yyyyMMdd";
131     else if (dateString.length() == 10 )
132       pattern = "yyyyMMddHH";
133     else if (dateString.length() == 12 )
134       pattern = "yyyyMMddHHmm";
135     else if (dateString.length() == 14 )
136       pattern = "yyyyMMddHHmmss";
137     else if (dateString.length() == 17 )
138       pattern = "yyyyMMddHHmmssSSS";
139     else
140       throw new ParseException JavaDoc("Input is not valid date string: " + dateString, 0);
141     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(pattern);
142     sdf.setTimeZone(GMT);
143     Date JavaDoc date = sdf.parse(dateString);
144     return date;
145   }
146   
147   /**
148    * Limit a date's resolution. For example, the date <code>2004-09-21 13:50:11</code>
149    * will be changed to <code>2004-09-01 00:00:00</code> when using
150    * <code>Resolution.MONTH</code>.
151    *
152    * @param resolution The desired resolution of the date to be returned
153    * @return the date with all values more precise than <code>resolution</code>
154    * set to 0 or 1
155    */

156   public static Date JavaDoc round(Date JavaDoc date, Resolution resolution) {
157     return new Date JavaDoc(round(date.getTime(), resolution));
158   }
159   
160   /**
161    * Limit a date's resolution. For example, the date <code>1095767411000</code>
162    * (which represents 2004-09-21 13:50:11) will be changed to
163    * <code>1093989600000</code> (2004-09-01 00:00:00) when using
164    * <code>Resolution.MONTH</code>.
165    *
166    * @param resolution The desired resolution of the date to be returned
167    * @return the date with all values more precise than <code>resolution</code>
168    * set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT
169    */

170   public static long round(long time, Resolution resolution) {
171     Calendar JavaDoc cal = Calendar.getInstance(GMT);
172
173     // protected in JDK's prior to 1.4
174
//cal.setTimeInMillis(time);
175

176     cal.setTime(new Date JavaDoc(time));
177     
178     if (resolution == Resolution.YEAR) {
179       cal.set(Calendar.MONTH, 0);
180       cal.set(Calendar.DAY_OF_MONTH, 1);
181       cal.set(Calendar.HOUR_OF_DAY, 0);
182       cal.set(Calendar.MINUTE, 0);
183       cal.set(Calendar.SECOND, 0);
184       cal.set(Calendar.MILLISECOND, 0);
185     } else if (resolution == Resolution.MONTH) {
186       cal.set(Calendar.DAY_OF_MONTH, 1);
187       cal.set(Calendar.HOUR_OF_DAY, 0);
188       cal.set(Calendar.MINUTE, 0);
189       cal.set(Calendar.SECOND, 0);
190       cal.set(Calendar.MILLISECOND, 0);
191     } else if (resolution == Resolution.DAY) {
192       cal.set(Calendar.HOUR_OF_DAY, 0);
193       cal.set(Calendar.MINUTE, 0);
194       cal.set(Calendar.SECOND, 0);
195       cal.set(Calendar.MILLISECOND, 0);
196     } else if (resolution == Resolution.HOUR) {
197       cal.set(Calendar.MINUTE, 0);
198       cal.set(Calendar.SECOND, 0);
199       cal.set(Calendar.MILLISECOND, 0);
200     } else if (resolution == Resolution.MINUTE) {
201       cal.set(Calendar.SECOND, 0);
202       cal.set(Calendar.MILLISECOND, 0);
203     } else if (resolution == Resolution.SECOND) {
204       cal.set(Calendar.MILLISECOND, 0);
205     } else if (resolution == Resolution.MILLISECOND) {
206       // don't cut off anything
207
} else {
208       throw new IllegalArgumentException JavaDoc("unknown resolution " + resolution);
209     }
210     return cal.getTime().getTime();
211   }
212
213   /** Specifies the time granularity. */
214   public static class Resolution {
215     
216     public static final Resolution YEAR = new Resolution("year");
217     public static final Resolution MONTH = new Resolution("month");
218     public static final Resolution DAY = new Resolution("day");
219     public static final Resolution HOUR = new Resolution("hour");
220     public static final Resolution MINUTE = new Resolution("minute");
221     public static final Resolution SECOND = new Resolution("second");
222     public static final Resolution MILLISECOND = new Resolution("millisecond");
223
224     private String JavaDoc resolution;
225
226     private Resolution() {
227     }
228     
229     private Resolution(String JavaDoc resolution) {
230       this.resolution = resolution;
231     }
232     
233     public String JavaDoc toString() {
234       return resolution;
235     }
236
237   }
238
239 }
240
Popular Tags