/**
* 获取日期
* 格式: 2020-10-12
*/
private static String getDateToday(int period){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,period);
return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
}
/**
* 获取周的第一天
* @param period 期数
* @return String
* **/
private static String getWeekStart(int period){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.WEEK_OF_MONTH, period);
cal.set(Calendar.DAY_OF_WEEK, 2);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
/**
* 获取周的最后一天
* 期次
* @return String
* **/
private static String getWeekEnd(int period){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.WEEK_OF_MONTH, period);
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
cal.add(Calendar.DAY_OF_WEEK, 1);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
/**
* 获取本月开始日期
* @return String
* **/
public static String getMonthStart(int period){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.MONTH, period);
cal.set(Calendar.DAY_OF_MONTH, 1);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
/**
* 获取本月最后一天
* @return String
* **/
public static String getMonthEnd(int period){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.MONTH, period);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}
/**
* 获取季度开始日期
* @return
*/
private static String getSeasonStart(int period) {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1+(period*3);
//根据月份获取所在季度
int quarter = new Double(Math.ceil(month / 3.0D)).intValue();
//所在季度的第一个月
int startMonth = quarter * 3 - 2;
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
a.set(Calendar.MONTH, startMonth - 1);
a.set(Calendar.DATE, 1);
return new SimpleDateFormat("yyyy-MM-dd").format(a.getTime());
}
/**
* 获取季度开始日期
* @return
*/
private static String getSeasonEnd(int period) {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1+(period*3);
//根据月份获取所在季度
int quarter = new Double(Math.ceil(month / 3.0D)).intValue();
//所在季度的第一个月
int startMonth = quarter * 3;
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
a.set(Calendar.MONTH, startMonth - 1);
a.set(Calendar.DATE, 1);
a.set(Calendar.DAY_OF_MONTH, a.getActualMaximum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat("yyyy-MM-dd").format(a.getTime());
}
/**
* 获取年的第一天
* @return String
* **/
public static String getYearStart(int period){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, period);
return new SimpleDateFormat("yyyy").format(cal.getTime())+"-01-01";
}
/**
* 获取本年的最后一天
* @return String
* **/
public static String getYearEnd(int period){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, period);
calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
}
/**
* 获取后半年的开始时间
*/
private static String getHalfYearStart(){
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, 6);
c.set(Calendar.DATE, 1);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
/**
* 获取前半年的结束时间
*/
private static String getHalfYearEnd(){
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}