指定月份显示每天的数据

简介: 指定月份显示每天的数据

实现功能截图image.png
1.前台传参

image.png
2.
/*

 * createTime  前台传字符串
 * @param indexNoticeDto
 * @return
 */
@Override
public List<IndexNoticeDto> selectIndexNotice(IndexNoticeDto indexNoticeDto) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    List<IndexNoticeDto> indexNoticeDtoList = new ArrayList<>();

    //不传月份日期表示当月,传日期表示之前月份
    String month = indexNoticeDto.getStrMonth();
    try {
        month = indexNoticeDto.getStrMonth();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (month == null) {
        indexNoticeDtoList.addAll(publicSqlMapper.selectIndexNotice(indexNoticeDto));
        List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList, null);
        return dayList;
    } else {
        indexNoticeDto.setStrStartTime(month.substring(0, month.length() - 3) + "-01");
        indexNoticeDto.setDtStartTime(sdf.parse(indexNoticeDto.getStrStartTime()));
        int year = Integer.parseInt(month.substring(0, 4));
        int month1 = Integer.parseInt(month.substring(5, 7));

        int max = LocalDate.of(year, month1, 1).lengthOfMonth();
        //月份最大天数
        indexNoticeDto.setStrEndTime(month.substring(0, month.length() - 3) + "-" + String.valueOf(max));
        indexNoticeDto.setDtEndTime(sdf.parse(indexNoticeDto.getStrEndTime()));

        indexNoticeDto.setStrStartTime("'" + month.substring(0, month.length() - 3) + "-01" + "'");
        indexNoticeDto.setStrEndTime("'" + month.substring(0, month.length() - 3) + "-" + String.valueOf(max) + "'");
        List<IndexNoticeDto> indexNoticeDtoList1 = publicSqlMapper.selectIndexNoticeHis(indexNoticeDto);
        List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList1, month.substring(0, month.length() - 3));
        return dayList;
    }
}
  1. 上月月末+当月全月+下月月初数据
    /**

    • 上月月末+当月全月+下月月初数据
    • @param date
    • @return
      */
      public List getMonthEveryDays(List list, String month) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

      int year = Integer.parseInt(month.substring(0, 4));
      int month1 = Integer.parseInt(month.substring(5, 7));
      //指定当前月份最大天数
      int max = LocalDate.of(year, month1, 1).lengthOfMonth();

      //当前月初开始日期属于周几
      int nowStartWeek = getWeekday(month + "-01");
      //当前月末结束日期属于周几
      int nowEndWeek = getWeekday(month + "-" + max);

      //上月年
      int lastYear = 0;
      //上月月份
      int lastMonth = Integer.parseInt(month.substring(5, 7)) - 1;
      String strLastMonth = "";
      if (lastMonth == 1) {

       lastMonth = 12;
       lastYear = Integer.parseInt(month.substring(0, 4)) - 1;
      

      } else {

       lastYear = year;
      

      }
      //上月最大天数
      int lastMonthMaxDay = LocalDate.of(lastYear, lastMonth, 1).lengthOfMonth();
      //拼接上月月末数据
      List lastIndexNoticeDtoList = new ArrayList<>();
      int sort = 0;
      //如果当前月份开始日期是周天,则不拼上月天数,我是根据前台的日期组件,一周开始属于周天拼接的
      if (nowStartWeek != 7) {

       for (int i = 0; i < nowStartWeek - 1; i++) {
           int lastDay = lastMonthMaxDay - i;
           IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
           if (lastMonth < 10) {
               indexNoticeDto.setCreateTime(lastYear + "-" + "0" + lastMonth + "-" + lastDay);
           } else {
               indexNoticeDto.setCreateTime(lastYear + "-" + lastMonth + "-" + lastDay);
           }
           indexNoticeDto.setDtCreateTime(sdf.parse(lastYear + "-" + lastMonth + "-" + lastDay));
           if (nowStartWeek == 1) {
               indexNoticeDto.setWeek(7);
           } else {
               indexNoticeDto.setWeek(nowStartWeek - (1 + i));
           }
           indexNoticeDto.setDay(lastDay);
           indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义
           sort = i;
           indexNoticeDto.setSort(sort);
           lastIndexNoticeDtoList.add(indexNoticeDto);
       }
      

      }

      //本月数据
      List indexNoticeDtoList = new ArrayList<>();
      //上月数据
      List collect1 = lastIndexNoticeDtoList.stream().sorted(Comparator.comparing(IndexNoticeDto::getSort).reversed()).collect(Collectors.toList());
      indexNoticeDtoList.addAll(collect1);
      Calendar calendar = Calendar.getInstance();

      for (int i = 0; i < max; i++) {

       IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
       if (month == null) {
           calendar.set(Calendar.DAY_OF_MONTH, i + 1);
       }
       String day = sdf.format(calendar.getTime());
       indexNoticeDto.setDay(i + 1);
       if (month != null) {
      
           int ii = i + 1;
           if (i < 9) {
               day = month + "-" + "0" + ii;
           } else {
               day = month + "-" + ii;
           }
       }
       indexNoticeDto.setCreateTime(day);
       indexNoticeDto.setDtCreateTime(sdf.parse(day));
       indexNoticeDto.setWeek(getWeekday(day));
       String finalDay = day;
       List<IndexNoticeDto> collect = list.stream().filter(s -> s.getCreateTime().equals(finalDay)).collect(Collectors.toList());
       if (collect.size() > 0) {
           indexNoticeDto.setTotal(collect.size());
       } else {
           indexNoticeDto.setTotal(0);
       }
       indexNoticeDto.setSort(sort + 1 + i);
      
       indexNoticeDtoList.add(indexNoticeDto);
      

      }

      //下个月数据
      int nextYear = 0;
      int nextMonth = 0;
      String strNextMonth = "";
      if (month1 == 12) {

       nextYear = year + 1;
       nextMonth = 1;
       strNextMonth = "0" + nextMonth;
      

      } else if (month1 != 12) {

       nextYear = year;
       nextMonth = month1 + 1;
       if (nextMonth < 1 && nextMonth < 10) {
           strNextMonth = "0" + nextMonth;
       } else {
           strNextMonth = String.valueOf(nextMonth);
       }
      

      }

      //日历组件方格 一共展示42天的数据
      int nextMonthDayNum = 42 - indexNoticeDtoList.size();
      for (int i = 0; i < nextMonthDayNum; i++) {

       String nextDay = "";
       if (i < 10) {
           nextDay = "0" + String.valueOf(i + 1);
       }
       IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
       indexNoticeDto.setCreateTime(nextYear + "-" + strNextMonth + "-" + nextDay);
       indexNoticeDto.setDtCreateTime(sdf.parse(nextYear + "-" + strNextMonth + "-" + nextDay));
       indexNoticeDto.setDay(i + 1);
       indexNoticeDto.setWeek(getWeekday(nextYear + "-" + strNextMonth + "-" + nextDay));
       indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义
       sort = i;
       indexNoticeDto.setSort(sort);
       indexNoticeDtoList.add(indexNoticeDto);
      

      }

      return indexNoticeDtoList;
      }

4.制定月份每一天数据,与数据库中的数据进行比较,有数据的时候total的值 >0

5.计算指定日期星期几

/*

 * 实现给定某日期,判断是星期几
 * 必须yyyy-MM-dd
 * @param date
 * @return
 */
public static int getWeekday(String date) {
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdw = new SimpleDateFormat("E");
    Date d = null;
    try {
        d = sd.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String strWeek = sdw.format(d); //输出 星期一。。。。。
    int intWeek = 0;
    switch (strWeek) {
        case "星期-":
            intWeek = 1;
            break;
        case "星期二":
            intWeek = 2;
            break;
        case "星期三":
            intWeek = 3;
            break;
        case "星期四":
            intWeek = 4;
            break;
        case "星期五":
            intWeek = 5;
            break;
        case "星期六":
            intWeek = 6;
            break;
        case "星期天":
            intWeek = 7;
            break;
        default:
            break;
    }
    return intWeek;
}

6.数据库查询

    SELECT
          total,
          day,
          CONCAT((date_format( ${strStartTime},'%yyy-')),(date_format( ${strStartTime},'%m-')), day) as create_time
    FROM
           (
             SELECT
                    DAY(create_time) as day,
                    count(notice_id) AS total
             FROM sys_notice
             WHERE create_time &gt;= ${strStartTime} and create_time  &lt;= ${strEndTime}
             GROUP BY DAY(create_time)
           ) as a
    WHERE a.day is not null

</select>

7.数据返回(一个日期组件一共展示42天的数据)
image.png

image.png

相关文章
|
Java
Java打印二进制
Java打印二进制
199 0
KTV点歌程序
KTV点歌程序
237 0
|
消息中间件 缓存 负载均衡
面试还不懂如何回答面试JVM相关的问题,看这一篇就够了
1.JVM常用的参数有哪些? 标准参数 -version -help -server -cp 3.1.2 -X参数 非标准参数,也就是在JDK各个版本中可能会变动
|
11月前
|
测试技术 C语言
单链表之无头链表(C语言版)
本文详细介绍了使用C语言实现无头单链表的方法,包括节点和链表结构的定义、链表的创建与销毁、节点的插入与删除,以及链表的打印等功能。文章通过具体的代码示例,展示了如何在无头链表中进行头插法、尾插法、自定义位置插入和删除,以及如何清空和销毁链表。
222 0
单链表之无头链表(C语言版)
|
消息中间件 人工智能 监控
RocketMQ体验测评
【8月更文挑战第15天】RocketMQ体验测评
210 1
|
设计模式 测试技术
依赖注入与工厂设计模式的区别
【8月更文挑战第22天】
241 0
|
XML 存储 缓存
记一次雪花算法遇到的 生产事故!
最近生产环境遇到一个问题: 现象:创建工单、订单等地方,全都创建数据失败。 初步排查:报错信息为duplicate key,意思是保存数据的时候,报主键 id 重复,而这些 id 都是由雪花算法生成的,按道理来说,雪花算法是生成分布式唯一 ID,不应该生成重复的 ID。
434 5
|
算法 调度
【完全复现】基于改进粒子群算法的微电网多目标优化调度
该文档描述了一个使用改进粒子群算法实现的微电网多目标优化调度的Matlab程序。该模型旨在最小化运行成本和环境保护成本,将多目标问题通过权值转换为单目标问题解决。程序中定义了决策变量,如柴油发电机、微型燃气轮机、联络线和储能的输出,并使用全局变量处理电负荷、风力和光伏功率等数据。算法参数包括最大迭代次数和种群大小。代码调用了`PSOFUN`函数来执行优化计算,并展示了优化结果的图表。
|
数据采集 自然语言处理 搜索推荐
使用Spyder进行动态网页爬取:实战指南
使用Spyder进行动态网页爬取:实战指南
火山中文编程 -- 判断循环
火山中文编程 -- 判断循环
100 1