一、当前年的上一年
核心代码:
public static String getYearBefore() {
SimpleDateFormat formats = new SimpleDateFormat("yyyy");
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
Date date = c.getTime();
return formats.format(date);
}
二、上一季度
核心代码:
/**
* 获取上一季度 开始和结束时间
*
* @return
*/
public static DateRange getLastQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}
/**
* 最小时间
*
* @param calendar
*/
private static void setMinTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
/**
* 最大时间
*
* @param calendar
*/
private static void setMaxTime(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
}
DateRange.java:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DateRange {
private Date start;
private Date end;
}
三、当月
核心代码:
/**
* 获取当月(开始时间)
*
* @return
*/
public static String getCurrentMonthStartDate() {
// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月第一天
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String firstday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
firstday = format.format(cale.getTime());
return firstday;
}
/**
* 获取当月(结束时间)
*
* @return
*/
public static String getCurrentMonthEndDate() {
// 获取当前年份、月份、日期
Calendar cale = null;
// 获取当月最后一天
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String lastday;
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
lastday = format.format(cale.getTime());
return lastday;
}
四、当季
核心代码:
/**
* 获取当季
*
* @return
*/
public static DateRange getThisQuarter() {
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
setMinTime(startCalendar);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
setMaxTime(endCalendar);
return new DateRange(startCalendar.getTime(), endCalendar.getTime());
}