Class DateTools
Provides 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 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 System.DateTime, just get the System.DateTime.Ticks and index this as a numeric value with Int64Field and use NumericRangeQuery<T> to query it.
Inheritance
Inherited Members
Namespace: Lucene.Net.Documents
Assembly: Lucene.Net.dll
Syntax
public static class DateTools
Methods
| Improve this Doc View SourceDateToString(DateTime, DateTools.Resolution)
Converts a System.DateTime to a string suitable for indexing.
Declaration
public static string DateToString(DateTime date, DateTools.Resolution resolution)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | the date to be converted |
DateTools.Resolution | resolution | the desired resolution, see Round(DateTime, DateTools.Resolution) |
Returns
Type | Description |
---|---|
System.String | a string in format |
Round(DateTime, DateTools.Resolution)
Limit a date's resolution. For example, the date 2004-09-21 13:50:11
will be changed to 2004-09-01 00:00:00
when using
MONTH.
Declaration
public static DateTime Round(DateTime date, DateTools.Resolution resolution)
Parameters
Type | Name | Description |
---|---|---|
System.DateTime | date | the date to be rounded |
DateTools.Resolution | resolution | The desired resolution of the date to be returned |
Returns
Type | Description |
---|---|
System.DateTime | the date with all values more precise than |
Round(Int64, DateTools.Resolution)
Limit a date's resolution. For example, the date 1095767411000
(which represents 2004-09-21 13:50:11) will be changed to
1093989600000
(2004-09-01 00:00:00) when using
MONTH.
Declaration
public static long Round(long time, DateTools.Resolution resolution)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | time | the time to be rounded |
DateTools.Resolution | resolution | The desired resolution of the date to be returned |
Returns
Type | Description |
---|---|
System.Int64 | the date with all values more precise than |
StringToDate(String)
Converts a string produced by TimeToString(Int64, DateTools.Resolution) or DateToString(DateTime, DateTools.Resolution) back to a time, represented as a System.DateTime object.
Declaration
public static DateTime StringToDate(string dateString)
Parameters
Type | Name | Description |
---|---|---|
System.String | dateString | the date string to be converted |
Returns
Type | Description |
---|---|
System.DateTime | the parsed time as a System.DateTime object |
Exceptions
Type | Condition |
---|---|
System.FormatException | if |
StringToTime(String)
Converts a string produced by TimeToString(Int64, DateTools.Resolution) or DateToString(DateTime, DateTools.Resolution) back to a time, represented as the number of milliseconds since January 1, 1970, 00:00:00 GMT (also known as the "epoch").
Declaration
public static long StringToTime(string dateString)
Parameters
Type | Name | Description |
---|---|---|
System.String | dateString | the date string to be converted |
Returns
Type | Description |
---|---|
System.Int64 | the number of milliseconds since January 1, 1970, 00:00:00 GMT (also known as the "epoch") |
Exceptions
Type | Condition |
---|---|
System.FormatException | if |
TimeToString(Int64, DateTools.Resolution)
Converts a millisecond time to a string suitable for indexing.
Declaration
public static string TimeToString(long time, DateTools.Resolution resolution)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | time | the date expressed as milliseconds since January 1, 1970, 00:00:00 GMT (also known as the "epoch") |
DateTools.Resolution | resolution | the desired resolution, see Round(Int64, DateTools.Resolution) |
Returns
Type | Description |
---|---|
System.String | a string in format |