public class DateTools extends Object
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
TermRangeQuery and PrefixQuery will 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 a Date or Calendar, just get the unix timestamp as
long using Date.getTime() or Calendar.getTimeInMillis() and
index this as a numeric value with LongField
and use NumericRangeQuery to query it.
| Modifier and Type | Class and Description |
|---|---|
static class |
DateTools.Resolution
Specifies the time granularity.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
dateToString(Date date,
DateTools.Resolution resolution)
Converts a Date to a string suitable for indexing.
|
static Date |
round(Date date,
DateTools.Resolution resolution)
Limit a date's resolution.
|
static long |
round(long time,
DateTools.Resolution resolution)
Limit a date's resolution.
|
static Date |
stringToDate(String dateString)
Converts a string produced by
timeToString or
dateToString back to a time, represented as a
Date object. |
static long |
stringToTime(String dateString)
Converts a string produced by
timeToString or
dateToString back to a time, represented as the
number of milliseconds since January 1, 1970, 00:00:00 GMT. |
static String |
timeToString(long time,
DateTools.Resolution resolution)
Converts a millisecond time to a string suitable for indexing.
|
public static String dateToString(Date date, DateTools.Resolution resolution)
date - the date to be convertedresolution - the desired resolution, see
round(Date, DateTools.Resolution)yyyyMMddHHmmssSSS or shorter,
depending on resolution; using GMT as timezonepublic static String timeToString(long time, DateTools.Resolution resolution)
time - the date expressed as milliseconds since January 1, 1970, 00:00:00 GMTresolution - the desired resolution, see
round(long, DateTools.Resolution)yyyyMMddHHmmssSSS or shorter,
depending on resolution; using GMT as timezonepublic static long stringToTime(String dateString) throws ParseException
timeToString or
dateToString back to a time, represented as the
number of milliseconds since January 1, 1970, 00:00:00 GMT.dateString - the date string to be convertedParseException - if dateString is not in the
expected formatpublic static Date stringToDate(String dateString) throws ParseException
timeToString or
dateToString back to a time, represented as a
Date object.dateString - the date string to be convertedParseException - if dateString is not in the
expected formatpublic static Date round(Date date, DateTools.Resolution resolution)
2004-09-21 13:50:11
will be changed to 2004-09-01 00:00:00 when using
Resolution.MONTH.resolution - The desired resolution of the date to be returnedresolution
set to 0 or 1public static long round(long time,
DateTools.Resolution resolution)
1095767411000
(which represents 2004-09-21 13:50:11) will be changed to
1093989600000 (2004-09-01 00:00:00) when using
Resolution.MONTH.resolution - The desired resolution of the date to be returnedresolution
set to 0 or 1, expressed as milliseconds since January 1, 1970, 00:00:00 GMTCopyright © 2000-2014 Apache Software Foundation. All Rights Reserved.