java在很多场合下会根据当前时间计算本季度的开始时间结束时间、当前月份的开始时间结束时间、当前周的开始时间以及结束时间。我这里整理下一个时间加工类,作为工具类记录下。
一、获取计算相关时间的方法如下:(如果有错误请指出,感谢!)
public class TimeUtils {
/**
* 初始化时间盒子
* @return timeBox
*/
public static TimeBox initTimeBox() throws ParseException {
TimeBox timeBox = new TimeBox();
Date currentDate = new Date();
timeBox.setCurrentDate(currentDate);
Date lastDate = getLastDate(currentDate);
timeBox.setLastDate(lastDate);
timeBox.setThisWeekStartDate(getCurrentWeekDateBegin(lastDate));
timeBox.setThisWeekEndDate(getCurrentWeekDateEnd(lastDate));
timeBox.setThisMonthStartDate(getCurrentMonthDateBegin(lastDate));
timeBox.setThisMonthEndDate(getCurrentMonthDateEnd(lastDate));
timeBox.setLastWeekStartDate(getLastWeekDateBegin(lastDate));
timeBox.setLastWeekEndDate(getLastWeekDateEnd(lastDate));
timeBox.setLastMonthStartDate(getLastMonthDateBegin(lastDate));
timeBox.setLastMonthEndDate(getLastMonthDateEnd(lastDate));
timeBox.setThisQuarterStartDate(getCurrentQuarterDateBegin(lastDate));
timeBox.setThisQuarterEndDate(getCurrentQuarterDateEnd(lastDate));
timeBox.setThisSundayDate(getCurrentWeekNumberDay(lastDate,0));
timeBox.setLastSundayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,0)));
timeBox.setThisMondayDate(getCurrentWeekNumberDay(lastDate,1));
timeBox.setLastMondayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,1)));
timeBox.setThisTuesdayDate(getCurrentWeekNumberDay(lastDate,2));
timeBox.setLastTuesdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,2)));
timeBox.setThisWednesdayDate(getCurrentWeekNumberDay(lastDate,3));
timeBox.setLastWednesdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,3)));
timeBox.setThisThursdayDate(getCurrentWeekNumberDay(lastDate,4));
timeBox.setLastThursdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,4)));
timeBox.setThisFridayDate(getCurrentWeekNumberDay(lastDate,5));
timeBox.setLastFridayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,5)));
timeBox.setThisSaturdayDate(getCurrentWeekNumberDay(lastDate,6));
timeBox.setLastSaturdayDate(getLastWeekDate(getCurrentWeekNumberDay(lastDate,6)));
//确认当前周几,获取本周的所有日期
timeBox.setStartDate(getEarliestDate(getLastMonthDateBegin(lastDate),getCurrentQuarterDateBegin(lastDate)));
timeBox.setEndDate(getCurrentQuarterDateEnd(lastDate));
return timeBox;
}
public static Date getEarliestDate(Date lastMonthStartDate, Date thisQuarterDateBegin) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = lastMonthStartDate;
if (getDayDiffer( simpleDateFormat.parse(simpleDateFormat.format(thisQuarterDateBegin)),simpleDateFormat.parse(simpleDateFormat.format(lastMonthStartDate))) > 0){
date = thisQuarterDateBegin;
}
return date;
}
/**
* 获取昨天日期
* @return date
*/
public static Date getLastDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,-1);
return calendar.getTime();
}
/**
* pass
* 返回当前月份的开始时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回当前月份的结束时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回上月的开始时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回上月的结束时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回当前周的开始时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回当前周的结束时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回上周的开始时间
* @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));
return calendar.getTime();
}
/**
* pass
* 返回上周的结束时间
* @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));
return calendar.getTime();
}
/**
* pass
* 返回当前周第几天的日期
* 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;
}
}
/**
* pass
* 返回上一周的当前时间
* @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();
}
/**
* pass
* 返回当前季度的开始时间
* @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);
return calendar.getTime();
}
/**
* pass
* 返回当前季度结束时间
* @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);
return calendar.getTime();
}
/**
* pass
* 某年某月是星期几
* @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;
}
/**
* pass
* 获取日期时间差
* @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));
}
}
二、TimeBox类
@Data
public class TimeBox {
Date currentDate;
Date lastDate;
Date thisQuarterStartDate;
Date thisQuarterEndDate;
Date thisMonthStartDate;
Date thisMonthEndDate;
Date lastMonthStartDate;
Date lastMonthEndDate;
Date thisWeekStartDate;
Date thisWeekEndDate;
Date lastWeekStartDate;
Date lastWeekEndDate;
Date thisSundayDate;
Date lastSundayDate;
Date thisMondayDate;
Date lastMondayDate;
Date thisTuesdayDate;
Date lastTuesdayDate;
Date thisWednesdayDate;
Date lastWednesdayDate;
Date thisThursdayDate;
Date lastThursdayDate;
Date thisFridayDate;
Date lastFridayDate;
Date thisSaturdayDate;
Date lastSaturdayDate;
Date startDate;
Date endDate;
@Override
public String toString() {
return "TimeBox{" +
"currentDate=" + currentDate + "\n" +
", lastDate=" + lastDate + "\n" +
", thisQuarterStartDate=" + thisQuarterStartDate + "\n" +
", thisQuarterEndDate=" + thisQuarterEndDate + "\n" +
", thisMonthStartDate=" + thisMonthStartDate + "\n" +
", thisMonthEndDate=" + thisMonthEndDate + "\n" +
", lastMonthStartDate=" + lastMonthStartDate + "\n" +
", lastMonthEndDate=" + lastMonthEndDate + "\n" +
", thisWeekStartDate=" + thisWeekStartDate + "\n" +
", thisWeekEndDate=" + thisWeekEndDate + "\n" +
", lastWeekStartDate=" + lastWeekStartDate + "\n" +
", lastWeekEndDate=" + lastWeekEndDate + "\n" +
", thisSundayDate=" + thisSundayDate + "\n" +
", lastSundayDate=" + lastSundayDate + "\n" +
", thisMondayDate=" + thisMondayDate + "\n" +
", lastMondayDate=" + lastMondayDate + "\n"+
", thisTuesdayDate=" + thisTuesdayDate + "\n" +
", lastTuesdayDate=" + lastTuesdayDate + "\n" +
", thisWednesdayDate=" + thisWednesdayDate + "\n" +
", lastWednesdayDate=" + lastWednesdayDate + "\n" +
", thisThursdayDate=" + thisThursdayDate + "\n" +
", lastThursdayDate=" + lastThursdayDate + "\n" +
", thisFridayDate=" + thisFridayDate + "\n" +
", lastFridayDate=" + lastFridayDate + "\n" +
", thisSaturdayDate=" + thisSaturdayDate + "\n" +
", lastSaturdayDate=" + lastSaturdayDate + "\n"+
", startDate=" + startDate + "\n"+
", endDate=" + endDate + "\n"+
'}';
}
public String myToString() {
SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
return "TimeBox{" +
"currentDate=" + sft.format(currentDate) + "\n" +
", lastDate=" + sft.format(lastDate )+ "\n" +
", thisQuarterStartDate=" + sft.format(thisQuarterStartDate) + "\n" +
", thisQuarterEndDate=" + sft.format(thisQuarterEndDate) + "\n" +
", thisMonthStartDate=" + sft.format(thisMonthStartDate) + "\n" +
", thisMonthEndDate=" + sft.format(thisMonthEndDate) + "\n" +
", lastMonthStartDate=" + sft.format(lastMonthStartDate) + "\n" +
", lastMonthEndDate=" + sft.format(lastMonthEndDate) + "\n" +
", thisWeekStartDate=" + sft.format(thisWeekStartDate) + "\n" +
", thisWeekEndDate=" + sft.format(thisWeekEndDate )+ "\n" +
", lastWeekStartDate=" + sft.format(lastWeekStartDate) + "\n" +
", lastWeekEndDate=" + sft.format(lastWeekEndDate) + "\n" +
", thisSundayDate=" + sft.format(thisSundayDate) + "\n" +
", lastSundayDate=" + sft.format(lastSundayDate) + "\n" +
", thisMondayDate=" + sft.format(thisMondayDate) + "\n" +
", lastMondayDate=" + sft.format(lastMondayDate) + "\n"+
", thisTuesdayDate=" + sft.format(thisTuesdayDate) + "\n" +
", lastTuesdayDate=" + sft.format(lastTuesdayDate) + "\n" +
", thisWednesdayDate=" + sft.format(thisWednesdayDate) + "\n" +
", lastWednesdayDate=" + sft.format(lastWednesdayDate) + "\n" +
", thisThursdayDate=" + sft.format(thisThursdayDate) + "\n" +
", lastThursdayDate=" + sft.format(lastThursdayDate) + "\n" +
", thisFridayDate=" + sft.format(thisFridayDate) + "\n" +
", lastFridayDate=" + sft.format(lastFridayDate) + "\n" +
", thisSaturdayDate=" + sft.format(thisSaturdayDate) + "\n" +
", lastSaturdayDate=" + sft.format(lastSaturdayDate) + "\n"+
", startDate=" + sft.format(startDate) + "\n"+
", endDate=" + sft.format(endDate) + "\n"+
'}';
}
}
三、调用
System.out.println(TimeUtils.initTimeBox().myToString())