Java版的Lucene对时间的处理和.Net版中的有很大不同,下面我给出org.apache.lucene.document包下DateTools类的.Net完整实现。
public class DateTools
{
private static readonly string YEAR_FORMAT = "yyyy";
private static readonly string MONTH_FORMAT = "yyyyMM";
private static readonly string DAY_FORMAT = "yyyyMMdd";
private static readonly string HOUR_FORMAT = "yyyyMMddHH";
private static readonly string MINUTE_FORMAT = "yyyyMMddHHmm";
private static readonly string SECOND_FORMAT = "yyyyMMddHHmmss";
private static readonly string MILLISECOND_FORMAT = "yyyyMMddHHmmssfff";
public static readonly DateTime BASE_UNIVERSAL_TIME = DateTime.Parse("January 1, 1970, 00:00:00 GMT").ToUniversalTime();
public static string TimeToString(long l, Resolution resolution)
{
DateTime dt = LongToDateTime(l);
string result = string .Empty ;
switch (resolution)
{
case Resolution.YEAR:
result = dt.ToString(YEAR_FORMAT);
break;
case Resolution.MONTH:
result = dt.ToString(MONTH_FORMAT);
break;
case Resolution.DAY:
result = dt.ToString(DAY_FORMAT);
break;
case Resolution.HOUR:
result = dt.ToString(HOUR_FORMAT);
break;
case Resolution.MINUTE:
result = dt.ToString(MINUTE_FORMAT);
break;
case Resolution.SECOND:
result = dt.ToString(SECOND_FORMAT);
break;
case Resolution.MILLISECOND:
result = dt.ToString(MILLISECOND_FORMAT);
break;
}
return result;
}
public static string TimeToString(DateTime time, Resolution resolution)
{
return TimeToString(DateTimeToLong(time), resolution);
}
public static long Round(long time, Resolution resolution)
{
DateTime dt = BASE_UNIVERSAL_TIME.AddMilliseconds(time).ToUniversalTime();
switch (resolution)
{
case Resolution.YEAR:
dt = dt.AddMonths(1 - dt.Month).AddDays(1 - dt.Day).AddHours(0 - dt.Hour).AddMinutes(0 - dt.Minute).AddSeconds(0 - dt.Second).AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.MONTH:
dt = dt.AddDays(1 - dt.Day).AddHours(0 - dt.Hour).AddMinutes(0 - dt.Minute).AddSeconds(0 - dt.Second).AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.DAY:
dt = dt.AddHours(0 - dt.Hour).AddMinutes(0 - dt.Minute).AddSeconds(0 - dt.Second).AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.HOUR:
dt = dt.AddMinutes(0 - dt.Minute).AddSeconds(0 - dt.Second).AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.MINUTE:
dt = dt.AddSeconds(0 - dt.Second) .AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.SECOND:
dt = dt.AddMilliseconds(0 - dt.Millisecond);
break;
case Resolution.MILLISECOND:
break;
}
return DateTimeToLong(dt);
}
public static DateTime Round(DateTime date, Resolution resolution)
{
return LongToDateTime(Round(DateTimeToLong(date), resolution));
}
public static long DateTimeToLong(DateTime time)
{
return (long)(time.ToUniversalTime() - BASE_UNIVERSAL_TIME).TotalMilliseconds;
}
public static DateTime LongToDateTime(long l)
{
return BASE_UNIVERSAL_TIME.AddMilliseconds(l).ToUniversalTime();
}
public enum Resolution
{
YEAR,
MONTH,
DAY,
HOUR,
MINUTE,
SECOND,
MILLISECOND
}
}
posted on 2007-05-17 12:00
Terry Liang 阅读(1678)
评论(2) 编辑 收藏 所属分类:
Lucene 2.1研究