Package org.apache.lucene.document
Class DateTools
- java.lang.Object
 - 
- org.apache.lucene.document.DateTools
 
 
- 
public class DateTools extends java.lang.ObjectProvides support for converting dates to strings and vice-versa. The strings are structured so that lexicographic sorting orders them by date, which makes them suitable for use as field values and search terms.This class also helps you to limit the resolution of your dates. Do not save dates with a finer resolution than you really need, as then
TermRangeQueryandPrefixQuerywill require more memory and become slower.Another approach is
NumericUtils, which provides a sortable binary representation (prefix encoded) of numeric values, which date/time are. For indexing aDateorCalendar, just get the unix timestamp aslongusingDate.getTime()orCalendar.getTimeInMillis()and index this as a numeric value withLongFieldand useNumericRangeQueryto query it. 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDateTools.ResolutionSpecifies the time granularity. 
- 
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringdateToString(java.util.Date date, DateTools.Resolution resolution)Converts a Date to a string suitable for indexing.static longround(long time, DateTools.Resolution resolution)Limit a date's resolution.static java.util.Dateround(java.util.Date date, DateTools.Resolution resolution)Limit a date's resolution.static java.util.DatestringToDate(java.lang.String dateString)Converts a string produced bytimeToStringordateToStringback to a time, represented as a Date object.static longstringToTime(java.lang.String dateString)Converts a string produced bytimeToStringordateToStringback to a time, represented as the number of milliseconds since January 1, 1970, 00:00:00 GMT.static java.lang.StringtimeToString(long time, DateTools.Resolution resolution)Converts a millisecond time to a string suitable for indexing. 
 - 
 
- 
- 
Method Detail
- 
dateToString
public static java.lang.String dateToString(java.util.Date date, DateTools.Resolution resolution)Converts a Date to a string suitable for indexing.- Parameters:
 date- the date to be convertedresolution- the desired resolution, seeround(Date, DateTools.Resolution)- Returns:
 - a string in format 
yyyyMMddHHmmssSSSor shorter, depending onresolution; using GMT as timezone 
 
- 
timeToString
public static java.lang.String timeToString(long time, DateTools.Resolution resolution)Converts a millisecond time to a string suitable for indexing.- Parameters:
 time- the date expressed as milliseconds since January 1, 1970, 00:00:00 GMTresolution- the desired resolution, seeround(long, DateTools.Resolution)- Returns:
 - a string in format 
yyyyMMddHHmmssSSSor shorter, depending onresolution; using GMT as timezone 
 
- 
stringToTime
public static long stringToTime(java.lang.String dateString) throws java.text.ParseExceptionConverts a string produced bytimeToStringordateToStringback to a time, represented as the number of milliseconds since January 1, 1970, 00:00:00 GMT.- Parameters:
 dateString- the date string to be converted- Returns:
 - the number of milliseconds since January 1, 1970, 00:00:00 GMT
 - Throws:
 java.text.ParseException- ifdateStringis not in the expected format
 
- 
stringToDate
public static java.util.Date stringToDate(java.lang.String dateString) throws java.text.ParseExceptionConverts a string produced bytimeToStringordateToStringback to a time, represented as a Date object.- Parameters:
 dateString- the date string to be converted- Returns:
 - the parsed time as a Date object
 - Throws:
 java.text.ParseException- ifdateStringis not in the expected format
 
- 
round
public static java.util.Date round(java.util.Date date, DateTools.Resolution resolution)Limit a date's resolution. For example, the date2004-09-21 13:50:11will be changed to2004-09-01 00:00:00when usingResolution.MONTH.- Parameters:
 resolution- The desired resolution of the date to be returned- Returns:
 - the date with all values more precise than 
resolutionset to 0 or 1 
 
- 
round
public static long round(long time, DateTools.Resolution resolution)Limit a date's resolution. For example, the date1095767411000(which represents 2004-09-21 13:50:11) will be changed to1093989600000(2004-09-01 00:00:00) when usingResolution.MONTH.- Parameters:
 resolution- The desired resolution of the date to be returned- Returns:
 - the date with all values more precise than 
resolutionset to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMT 
 
 - 
 
 -