查詢指定年月的數據
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);
}
}