com.gitee.cn9750wang.webtools.query.TimeRange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of web-tools Show documentation
Show all versions of web-tools Show documentation
web tools for spring-boot web project
The newest version!
/*
* Copyright 2021 wwy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitee.cn9750wang.webtools.query;
import com.gitee.cn9750wang.webtools.error.P21Exception;
import com.gitee.cn9750wang.webtools.error.defines.ParamErr;
import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 时间范围查询对象
*
* @author wwy
*/
public final class TimeRange implements Serializable {
private static final long serialVersionUID = 1L;
/** 开始时间 */
private final LocalDateTime start;
/** 结束时间 */
private final LocalDateTime end;
/**
* 字符串构造时间范围
* @param timeRangeStr 时间范围字符串,支持以下几种格式:
* 1. yyyy
* 2. yyyy-MM
* 3. yyyy-MM-dd
* 4. yyyy-MM-ddTHH
* 5. yyyy-MM-ddTHH:mm
* 6. yyyy~yyyy
* 7. yyyy-MM~yyyy-MM
* 8. yyyy-MM-dd~yyyy-MM-dd
* 9. yyyy-MM-ddTHH~yyyy-MM-ddTHH
* 10.yyyy-MM-ddTHH:mm~yyyy-MM-ddTHH:mm
*/
public TimeRange(String timeRangeStr) {
// 非空检查
ParamErr.NOT_NULL.notBlank(timeRangeStr,"时间范围");
var s = StringUtils.trim(timeRangeStr);
final int length = s.length();
switch (length){
// 1. yyyy
case 4:
start = get(s);
end = start.plusYears(1);
break;
// 2. yyyy-MM
case 7:
start = get(s);
end = start.plusMonths(1);
break;
// 3. yyyy-MM-dd
case 10:
start = get(s);
end = start.plusDays(1);
break;
// 4. yyyy-MM-ddTHH
case 13:
start = get(s);
end = start.plusHours(1);
break;
// 5. yyyy-MM-ddTHH:mm
case 16:
start = get(s);
end = start.plusMinutes(1);
break;
/* 以下情况为精确调整,通过分割字符串并进行设置 */
// 6. yyyy~yyyy
case 9:
// 7. yyyy-MM~yyyy-MM
case 15:
// 8. yyyy-MM-dd~yyyy-MM-dd
case 21:
// 9.yyyy-MM-ddTHH~yyyy-MM-ddTHH
case 27:
// 10.yyyy-MM-dd-HH:mm~yyyy-MM-dd-HH:mm
case 33:
var split = s.split("~");
ParamErr.ILLEGAL.isTrue(split.length == 2,"时间范围");
start = get(split[0]);
end = get(split[1]);
break;
default:
ParamErr.ILLEGAL.format("时间范围").throwErr();
start = LocalDateTime.MIN;
end = LocalDateTime.MAX;
}
}
private static LocalDateTime get(String str){
int year = parse(str,0,4);
int month = parse(str,5,7);
int day = parse(str,8,10);
int hour = parse(str,11,13);
int minute = parse(str,14,16);
ParamErr.ILLEGAL.isTrue(year != -1,"年份");
month = month == -1 ? 1 : month;
ParamErr.BETWEEN.isTrue(month >= 1 && month <= 12,"月份",1,12);
day = day == -1 ? 1 : day;
ParamErr.BETWEEN.isTrue(day >= 1 && day <= 31,"日期",1,31);
hour = hour == -1 ? 0 : hour;
ParamErr.BETWEEN.isTrue(hour >= 0 && hour <= 23,"小时",0,23);
minute = minute == -1 ? 0 : minute;
ParamErr.BETWEEN.isTrue(minute >= 0 && minute <= 59,"分钟",0,59);
try {
return LocalDateTime.of(year,month,day,hour,minute);
}catch (Exception e){
throw new P21Exception(e);
}
}
private static int parse(String str,int start,int end){
var s = StringUtils.substring(str,start,end);
if(StringUtils.isBlank(s)){
return -1;
}
try {
return Integer.parseInt(s);
}catch (Exception e){
ParamErr.PATTERN.format(s).throwErr();
return -1;
}
}
public LocalDateTime getStart() {
return start;
}
public LocalDateTime getEnd() {
return end;
}
@Override
public String toString() {
return "TimeRange{" +
"start=" + start +
", end=" + end +
'}';
}
}