• 查找正序排列的List中缺失的日期数据的一个算法


    Code:

            public List<DateTime> getMissDateData()
            {
    
                DateTime[] keys = { DateTime.Now.AddDays(-5), DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-1), };
                List<DateTime> missingDate = new List<DateTime>();
                for (int i = 0; i < keys.Length; i++)
                {
                    logger.Info("Orginal data:"+keys[i]);
                }
    
                if (keys.Length > 0)
                {
                    DateTime firstDate = keys[0];
                    if (firstDate.Day > 1)
                    {
                        int missingDateInt = firstDate.Day - 1;
                        for (int i = missingDateInt; i > 0; i--)
                        {
                            missingDate.Add(firstDate.AddDays(-i));
                        }
                    }
                    int keysLength = (keys[keys.Length - 1] - keys[0]).Days;
    
                    DateTime dtTemp = keys[0];
                    // firstDate = keys[0];
                    int count = keys.Length;
                    for (int i = 0; i < count; i++)
                    {
                        Console.WriteLine(dtTemp.ToString(keys[i].ToString("yyyy-MM-dd HH:mm:ss")));
                        while (DateTime.Compare(keys[i], dtTemp) != 0)
                        {
                            logger.InfoFormat("Actual Date:{0};  The date of the cumulative:{1}", keys[i].ToShortDateString(), dtTemp.ToShortDateString());
                            logger.InfoFormat("{0}", DateTime.Compare(keys[i], dtTemp));
                            //Console.WriteLine(String.Format("Actual Date:{0};  The date of the cumulative:{1}", keys[i].ToShortDateString(), dtTemp.ToShortDateString()));
                           // Console.WriteLine(String.Format("{0}", DateTime.Compare(keys[i], dtTemp)));
                            missingDate.Add(dtTemp);
                            dtTemp = dtTemp.AddDays(1);
                        }
                        dtTemp = dtTemp.AddDays(1);
                    }
                }
                else
                {
                    logger.InfoFormat("Has no new data:{0}", keys.Length);
                   // Console.WriteLine(String.Format("Has no new data:{0}", keys.Length));
                }
    
                for (int i = 0; i < missingDate.Count; i++)
                {
                    logger.Info("Missing data:" + missingDate[i].ToShortDateString());
                    //Console.WriteLine("Missing data:" + missingDate[i].ToShortDateString());
                }
                return missingDate;
            }
    欢迎大家吐槽。
  • 相关阅读:
    Java检测文件是否UTF8编码
    Linux: uid/euid/suid的关系
    位移运算
    Springmvc 重定向参数传递方式
    @RequestBody和@RequestParam区别
    jsp页面老提示Multiple annotations found at this line:
    滚动表格代码
    滚动条样式修改
    WebService的四种客户端调用方式
    table元素的td和ul元素li隔行变色
  • 原文地址:https://www.cnblogs.com/softidea/p/3425628.html
Copyright © 2020-2023  润新知