根据传入的日期获取开始时间和结束时间,到时分秒级别
如果不要时分秒,SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
格式即可
public class TimeUtil {
public static void main(String[] args) {
System.out.println("-------------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(getCurrentYearDateEnd(new Date())));
}
/**
* 获取昨天日期
* @return date
*/
public static Date getLastDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,-1);
return calendar.getTime();
}
/**
* 返回当前月份的开始时间
* @return Date
*/
public static Date getCurrentMonthDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.MONTH,0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回当前月份的结束时间
* @return Date
*/
public static Date getCurrentMonthDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,lastDay);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 返回上月的开始时间
* @return Date
*/
public static Date getLastMonthDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.MONTH,-1);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回上月的结束时间
* @return Date
*/
public static Date getLastMonthDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.MONTH,0);
calendar.add(Calendar.DATE,-1);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 返回当前周的开始时间
* @return Date
*/
public static Date getCurrentWeekDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dateBegin = calendar.getActualMinimum(Calendar.DAY_OF_WEEK);
calendar.set(Calendar.DAY_OF_WEEK,dateBegin);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回当前周的结束时间
* @return Date
*/
public static Date getCurrentWeekDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dateEnd = calendar.getActualMaximum(Calendar.DAY_OF_WEEK);
calendar.set(Calendar.DAY_OF_WEEK,dateEnd);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 返回上周的开始时间
* @return Date
*/
public static Date getLastWeekDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_MONTH,-1);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回上周的结束时间
* @return Date
*/
public static Date getLastWeekDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_MONTH,-1);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 返回当前周第几天的日期
* 0 为当前周的周日
* 1 ~ 6 为当前周的周一到周六
* @return Date
*/
public static Date getCurrentWeekNumberDay(Date date, int num){
if (num >= 0 && num <= 6){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
calendar.add(Calendar.DAY_OF_WEEK,num);
return calendar.getTime();
}else {
return null;
}
}
/**
* 返回上一周的当前时间
* @return Date
*/
public static Date getLastWeekDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_WEEK,-7);
return calendar.getTime();
}
/**
* 返回当前季度的开始时间
* @return Date
*/
public static Date getCurrentQuarterDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
int quarter = month / 3 + 1;
int startMonth = 1;
if (quarter == 2){
startMonth = 4;
}else if(quarter == 3){
startMonth = 7;
}else if(quarter == 4){
startMonth = 10;
}
calendar.set(Calendar.MONTH,startMonth - 1);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回当前季度结束时间
* @return Date
*/
public static Date getCurrentQuarterDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
int quarter = month / 3 + 1;
int endMonth = 3;
if (quarter == 2){
endMonth = 6;
}else if(quarter == 3){
endMonth = 9;
}else if(quarter == 4){
endMonth = 12;
}
calendar.set(Calendar.MONTH,endMonth - 1);
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,lastDay);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 返回当前年度的开始时间
* @return Date
*/
public static Date getCurrentYearDateBegin(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH,0);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 返回当前年度结束时间
* @return Date
*/
public static Date getCurrentYearDateEnd(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH,11);
calendar.set(Calendar.DAY_OF_MONTH,31);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 某年某月是星期几
* @param date date
* @return (周日返回0,周一到周六就是1-6)
*/
public static int getWeek(Date date) {
Calendar calendar = Calendar.getInstance();
// 设置
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 获取日期时间差
* @param startDate 开始时间
* @param endDate 结束时间
* @return day
* @throws ParseException
*/
public static int getDayDiffer(Date startDate, Date endDate) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
}
}