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())