81 lines
3.1 KiB
Java
81 lines
3.1 KiB
Java
package net.shapelight.common.utils;
|
|
|
|
import java.sql.Time;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
|
|
public class MyDateUtils {
|
|
|
|
public static String getCurrentDayStartTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar cal = Calendar.getInstance();
|
|
Date mondayDate = cal.getTime();
|
|
String weekBegin = sdf.format(mondayDate)+" 00:00:00";
|
|
return weekBegin;
|
|
}
|
|
|
|
public static String getCurrentDayEndTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar cal = Calendar.getInstance();
|
|
Date mondayDate = cal.getTime();
|
|
String weekBegin = sdf.format(mondayDate)+" 23:59:59";
|
|
return weekBegin;
|
|
}
|
|
|
|
public static String getCurrentWeekStartTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar cal = Calendar.getInstance();
|
|
//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
|
|
cal.setFirstDayOfWeek(Calendar.MONDAY);
|
|
//获得当前日期是一个星期的第几天
|
|
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
|
|
if(dayWeek==1){
|
|
dayWeek = 8;
|
|
}
|
|
|
|
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
|
|
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
|
|
Date mondayDate = cal.getTime();
|
|
String weekBegin = sdf.format(mondayDate)+" 00:00:00";
|
|
return weekBegin;
|
|
}
|
|
|
|
public static String getCurrentWeekEndTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar cal = Calendar.getInstance();
|
|
//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
|
|
cal.setFirstDayOfWeek(Calendar.MONDAY);
|
|
//获得当前日期是一个星期的第几天
|
|
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
|
|
if(dayWeek==1){
|
|
dayWeek = 8;
|
|
}
|
|
cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
|
|
Date sundayDate = cal.getTime();
|
|
String weekEnd = sdf.format(sundayDate)+" 23:59:59";
|
|
return weekEnd;
|
|
}
|
|
|
|
public static String getCurrentMonthStartTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(new Date());
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
|
|
return sdf.format(calendar.getTime())+" 00:00:00";
|
|
}
|
|
|
|
public static String getCurrentMonthEndTime(){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar calendar2 = Calendar.getInstance();
|
|
calendar2.setTime(new Date());
|
|
calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
return sdf.format(calendar2.getTime())+" 23:59:59";
|
|
}
|
|
|
|
public static int getSeconds(Time time){
|
|
int sec = time.getHours() * 3600 + time.getMinutes()*60 + time.getSeconds();
|
|
return sec;
|
|
}
|
|
}
|