查询指定年月的数据
mybatis中的SQL语句:
select count(*) from test
where DATE_FORMAT(start_date,'%Y-%m')>=#{attenceDate} and DATE_FORMAT(start_date,'%Y-%m')<![CDATA[ <= ]]>#{attenceDate}
mysql中的SQL语句:
select count(*) from test
where DATE_FORMAT(start_date,'%Y-%m')>='2020-02' and DATE_FORMAT(start_date,'%Y-%m')<='2020-02'
java日期计算工具类
package com.tl.ie.model.attence.util; import com.tl.ie.model.attence.dto.PunchClockDto; import com.tl.ie.model.attence.request.PunchClockRequest; import com.tl.ie.model.xz.Roster; import org.springframework.beans.BeanUtils; import javax.xml.crypto.Data; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * @author mengziyang * @date 2021/1/8 */ public class DateAttenceUtil { /*日期时间比大小*/ public static int timeCompare(String t1,String t2){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Calendar c1=Calendar.getInstance(); Calendar c2=Calendar.getInstance(); try { c1.setTime(formatter.parse(t1)); c2.setTime(formatter.parse(t2)); } catch (ParseException e) { e.printStackTrace(); } int result=c1.compareTo(c2); return result; } /*月份时间比大小*/ public static int timeMonthCompare(String t1,String t2){ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); Calendar c1=Calendar.getInstance(); Calendar c2=Calendar.getInstance(); try { c1.setTime(formatter.parse(t1)); c2.setTime(formatter.parse(t2)); } catch (ParseException e) { e.printStackTrace(); } int result=c1.compareTo(c2); return result; } /** * 获取指定年月的第一天 yyyy-MM-dd HH:mm:ss * @param year * @param month * @return */ public static String getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getMinimum(Calendar.DATE); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH,firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // String date =sdf.format(cal.getTime()); String date = dayFirstTime(cal.getTime()); return date; } /** * 获取指定年月的最后一天 yyyy-MM-dd HH:mm:ss * @param year * @param month * @return */ public static String getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DATE); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = sdf.format(cal.getTime()); return date; } /** * 获取指定年月的第一天 yyyy-MM-dd * @param year * @param month * @return */ public static String getFirstDayOfMonth1(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最小天数 int firstDay = cal.getMinimum(Calendar.DATE); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH,firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date =sdf.format(cal.getTime()); return date; } /** * 获取指定年月的最后一天 yyyy-MM-dd * @param year * @param month * @return */ public static String getLastDayOfMonth1(int year, int month) { Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DATE); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(cal.getTime()); return date; } /** * 获取指定年月的每一天 * @param year * @param month * @param day * @return */ public static List<String> getDayListOfMonth(int year,int month,int day) { List<String> list = new ArrayList<String>(); Calendar aCalendar = Calendar.getInstance(Locale.CHINA); year = aCalendar.get(Calendar.YEAR);//年份 month = aCalendar.get(Calendar.MONTH) + 1;//月份 day = aCalendar.getActualMaximum(Calendar.DATE); for (int i = 1; i <= day; i++) { String aDate = ""; if(i<10 && month<10){ aDate = String.valueOf(year)+"-0"+month+"-0"+i; }else if(i<10){ aDate = String.valueOf(year)+"-"+month+"-0"+i; }else if(month<10){ aDate = String.valueOf(year)+"-0"+month+"-"+i; }else{ aDate = String.valueOf(year)+"-"+month+"-"+i; } list.add(aDate); } return list; } /** * 计算两个日期之间相差的天数 * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public static int daysBetween(Date smdate, Date bdate) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /**计算两个日期之间相差的天数 * *字符串的日期格式的计算 */ public static int daysBetween(String smdate,String bdate){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); long between_days=0; try { cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); between_days=(time2-time1)/(1000*3600*24); } catch (ParseException e) { e.printStackTrace(); } return Integer.parseInt(String.valueOf(between_days)); } /**计算两个日期之间相差的天数 * *字符串的日期格式的计算 */ public static int toYear(Data data){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy"); return Integer.parseInt(sdf.format(data)); } public static int toMonth(Data data){ SimpleDateFormat sdf=new SimpleDateFormat("MM"); return Integer.parseInt(sdf.format(data)); } public static int toDay(Data data){ SimpleDateFormat sdf=new SimpleDateFormat("dd"); return Integer.parseInt(sdf.format(data)); } /** * 获取两个时间中的每一天 * @param startTime * @param endTime * @return */ public static List<Date> getPerDay(Date startTime,Date endTime ){ //定义一个接受时间的集合 List<Date> lDate = new ArrayList<Date>(); lDate.add(startTime); Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calBegin.setTime(startTime); Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calEnd.setTime(endTime); // 测试此日期是否在指定日期之后 while (endTime.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1); lDate.add(calBegin.getTime()); } return lDate; } /** * 日期转星期 * */ public static String dateToWeek(String datetime) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" }; Calendar cal = Calendar.getInstance(); // 获得一个日历 Date datet = null; try { datet = f.parse(datetime); cal.setTime(datet); } catch (ParseException e) { e.printStackTrace(); } int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 if (w < 0) w = 0; return weekDays[w]; } /** * 日期转年 * */ public static String DTSdateToYear(Date datetime) { SimpleDateFormat f = new SimpleDateFormat("yyyy"); String datet = null; datet = f.format(datetime); return datet; } public static String dayFirstTime(Date d){ DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = format.format(d); Date d2 = null; try { d2 = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } System.out.println(format2.format(d2)); int dayMis = 1000 * 60 * 60 * 24;// 一天的毫秒-1 // 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 long curMillisecond = d2.getTime();// 当天的毫秒 long resultMis = curMillisecond + (dayMis - 1); // 当天最后一秒 // 得到我须要的时间 当天最后一秒 Date resultDate = new Date(resultMis); System.out.println(format2.format(resultDate)); return format2.format(d2); } public static String dayLastTime(Date d){ DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = format.format(d); Date d2 = null; try { d2 = format.parse(str); } catch (ParseException e) { e.printStackTrace(); } System.out.println(format2.format(d2)); int dayMis = 1000 * 60 * 60 * 24;// 一天的毫秒-1 // 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 long curMillisecond = d2.getTime();// 当天的毫秒 long resultMis = curMillisecond + (dayMis - 1); // 当天最后一秒 // 得到我须要的时间 当天最后一秒 Date resultDate = new Date(resultMis); System.out.println(format2.format(resultDate)); return format2.format(resultDate); } }