指定月份显示每天的数据

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

实现功能截图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

相关文章
|
4月前
指定月份计算最大天数
指定月份计算最大天数
|
4月前
日期工具,校验当年开始年份,结束年份,当月开始日期,结合素日期
日期工具,校验当年开始年份,结束年份,当月开始日期,结合素日期
|
11月前
题目:从键盘输入月份的英文速写,程序显示数字月份。例如输入“May“, 则程序显示“May是5月份“。
题目:从键盘输入月份的英文速写,程序显示数字月份。例如输入“May“, 则程序显示“May是5月份“。
题目:从键盘输入月份的英文速写,程序显示数字月份。例如输入“May“, 则程序显示“May是5月份“。
wustojc4005求月份对应的英文名称及天数
wustojc4005求月份对应的英文名称及天数
26 0
|
JavaScript 前端开发
javascript以当前日期为准计算当月、上月、下月直接输出日期的解决方案
javascript以当前日期为准计算当月、上月、下月直接输出日期的解决方案
81 0
判断月份天数
判断月份天数
47 0
|
Java 程序员
Java 案例练习:编写 Java 程序,输入年份和月份,使用 switch 结构计算对应月份的天数。月份为 1、3、5、7、8、10、12 时,天数为 31 天。月份为 4、6、9、11 时,天数为
Java 案例练习:编写 Java 程序,输入年份和月份,使用 switch 结构计算对应月份的天数。月份为 1、3、5、7、8、10、12 时,天数为 31 天。月份为 4、6、9、11 时,天数为
484 0
Java 案例练习:编写 Java 程序,输入年份和月份,使用 switch 结构计算对应月份的天数。月份为 1、3、5、7、8、10、12 时,天数为 31 天。月份为 4、6、9、11 时,天数为
【C#每日一题】输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历
作业1:输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历 运行结果: 上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { .
152 0
【C#每日一题】输入任意一个日期显示出它是当年的第几天?星期几?并打印出当月的日历
生成以周统计的表头,跨月份的周算在后一个月
这是人力统计的一个表格的表头,根据月份,划分周,每周从周一开始到周日(国内习惯性)。而跨月份的周算在前一个月还是后一个月,我们的需求是算在后一个月。根据情况而定。
231 0
生成以周统计的表头,跨月份的周算在后一个月