时间筛选框的处理和参数校验
- 如果前端使用筛选框的筛选选项则传到后端再处理时间从而获得时间戳
- 如果前端使用筛选框的自定义时间则使用由前端处理后传进来的时间戳
请求参数工具类
public class ReqParamUtils {
private ReqParamUtils() {
}
public static Map<String, Object> setTimeInterval(String timeOption, long firstTime, long endTime){
Map<String, Object> timeParams = new HashMap<>();
if (firstTime != 0 && endTime != 0){
timeParams.put("firstTime", firstTime);
timeParams.put("endTime", endTime);
return timeParams;
}
if (StringUtils.equals(timeOption, TimeTypeEnum.ALL.getType())){
return timeParams;
}else {
endTime = System.currentTimeMillis();
firstTime = 0;
if (StringUtils.equals(timeOption, TimeTypeEnum.ONE_WEEK.getType())){
firstTime = dateDifference(endTime, Calendar.DATE, CommonConst.N_MINUS_SEVEN);
}
else if (StringUtils.equals(timeOption, TimeTypeEnum.ONE_MONTH.getType())){
firstTime = dateDifference(endTime, Calendar.MONTH, CommonConst.N_MINUS_ONE);
}
else if (StringUtils.equals(timeOption, TimeTypeEnum.THREE_MONTH.getType())){
firstTime = dateDifference(endTime, Calendar.MONTH, CommonConst.N_MINUS_THREE);
}
timeParams.put("firstTime", firstTime / CommonConst.N_THOUSAND);
timeParams.put("endTime", endTime / CommonConst.N_THOUSAND);
}
return timeParams;
}
private static long dateDifference(long nowTime, int calendarType, int interval){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(nowTime));
calendar.add(calendarType, interval);
return calendar.getTimeInMillis();
}
private static boolean timeValidated(String timeOption, long firstTime, long endTime){
return (StringUtils.isBlank(timeOption) && firstTime != 0 && endTime != 0)
|| (StringUtils.isNotBlank(timeOption) && firstTime == 0 && endTime == 0);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
时间筛选枚举类
public enum TimeTypeEnum {
NO_USE("", "未使用时间筛选"),
ALL("all", "全部"),
ONE_WEEK("oneWeek", "近一周"),
ONE_MONTH("oneMonth", "近一个月"),
THREE_MONTH("threeMonth", "近三个月");
private final String type;
private final String desc;
TimeTypeEnum(String type, String desc) {
this.type = type;
this.desc = desc;
}
public String getType() {
return type;
}
public String getDesc() {
return desc;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
常量类
public interface CommonConst {
int N_MINUS_SEVEN = -7;
int N_MINUS_THREE = -3;
int N_MINUS_ONE = -1;
int N_ZERO = 0;
int N_ONE = 1;
int N_TWO = 2;
int N_THOUSAND = 1000;
}