随笔 - 17  文章 - 84  trackbacks - 0
<2007年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

如非特别说明,所有文章均为原创。如需引用,请注明出处
Email:liangtianyu@gmail.com
MSN:terry.liangtianyu@hotmail.com

常用链接

留言簿(4)

随笔分类(12)

随笔档案(17)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 51484
  • 排名 - 960

最新评论

阅读排行榜

评论排行榜

   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研究

FeedBack:
# re: 基于Lucene 2.1研究:时间的处理 2007-07-12 10:53 heavenking
谁给我讲讲“private static readonly”这一句是啥意思?????小第初学!谢谢了。
  回复  更多评论
  
# re: 基于Lucene 2.1研究:时间的处理 2007-07-12 11:01 Terry Liang
@heavenking
私有的静态的只读变量
私有:此变量只在类内部可以访问
静态:此变量只会在类初始化时创建一次
只读:运行时不可以改变此变量的值  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: