Java-获取上月、上季度、本年度时间段
public static void main(String[] args) {
BigScreenDto dto = new BigScreenDto();
dto.setDateType(1);
System.out.println(JSON.toJSONString(getDateBetween(dto)));
dto.setDateType(2);
System.out.println(JSON.toJSONString(getDateBetween(dto)));
dto.setDateType(3);
System.out.println(JSON.toJSONString(getDateBetween(dto)));
}
/**
* 获取上月、上季度、本年度
* @param dto
* @return
*/
private static String[] getDateBetween(BigScreenDto dto) {
Preconditions.checkArgument(dto.getDateType() != null, "dateType不能为空,1、2、3分别对应上月、上季度、本年度");
String[] date = new String[2];
DateTime startDate;
DateTime endDate;
switch (dto.getDateType()) {
case 1:
DateTime oneDayOfLastMonth = DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()), -2);
startDate = DateUtil.beginOfMonth(oneDayOfLastMonth);
endDate = DateUtil.endOfMonth(oneDayOfLastMonth);
date[0] = startDate.toString(DATETIME_FORMAT);
date[1] = endDate.toString(DATETIME_FORMAT);
break;
case 2:
DateTime oneDayOfLastQuarter = DateUtil.offsetDay(DateUtil.beginOfQuarter(new Date()), -2);
startDate = DateUtil.beginOfQuarter(oneDayOfLastQuarter);
endDate = DateUtil.endOfQuarter(oneDayOfLastQuarter);
date[0] = startDate.toString(DATETIME_FORMAT);
date[1] = endDate.toString(DATETIME_FORMAT);
break;
case 3:
startDate = DateUtil.beginOfYear(new Date());
endDate = DateUtil.endOfYear(new Date());
date[0] = startDate.toString(DATETIME_FORMAT);
date[1] = endDate.toString(DATETIME_FORMAT);
break;
default:
throw new BizzException("dateType不正确,1、2、3分别对应上月、上季度、本年度");
}
return date;
}
正文到此结束
- 本文标签: Spring Spring Boot
- 本文链接: https://code.jiangjiesheng.cn/article/215
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。