火点重构-完善防火季订阅
This commit is contained in:
parent
298d76662c
commit
be55e7b47a
@ -1,6 +1,5 @@
|
|||||||
package com.xkrs.dao;
|
package com.xkrs.fireseason;
|
||||||
|
|
||||||
import com.xkrs.model.entity.FireSeasonConfigEntity;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
@ -1,4 +1,4 @@
|
|||||||
package com.xkrs.model.entity;
|
package com.xkrs.fireseason;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -101,13 +101,6 @@ public class FireSeasonConfigEntity implements Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "FireSeasonConfigEntity{" +
|
return "FireSeasonConfigEntity{" + "id=" + id + ", configLevel=" + configLevel + ", startTime='" + startTime + '\'' + ", endTime='" + endTime + '\'' + ", countyCode='" + countyCode + '\'' + ", countyName='" + countyName + '\'' + '}';
|
||||||
"id=" + id +
|
|
||||||
", configLevel=" + configLevel +
|
|
||||||
", startTime='" + startTime + '\'' +
|
|
||||||
", endTime='" + endTime + '\'' +
|
|
||||||
", countyCode='" + countyCode + '\'' +
|
|
||||||
", countyName='" + countyName + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.xkrs.fireseason;
|
||||||
|
|
||||||
|
public interface FireSeasonConfigService {
|
||||||
|
|
||||||
|
String updateFireSeasonConfig();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
package com.xkrs.fireseason;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.xkrs.dao.FirePointChannelConfigDao;
|
||||||
|
import com.xkrs.dao.FirePointOrdinaryDao;
|
||||||
|
import com.xkrs.dao.StreetDao;
|
||||||
|
import com.xkrs.dao.SysUserDao;
|
||||||
|
import com.xkrs.helper.FirePointPushManager;
|
||||||
|
import com.xkrs.helper.FirePointQueryManager;
|
||||||
|
import com.xkrs.utilsnew.CountyCodeUtils;
|
||||||
|
import com.xkrs.utilsnew.DateTimeUtils;
|
||||||
|
import com.xkrs.utilsnew.ListUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.i18n.LocaleContextHolder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.xkrs.utilsold.HttpClientUtils.sendHttpPostTextPlain;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FireSeasonConfigServiceImpl implements FireSeasonConfigService {
|
||||||
|
|
||||||
|
public static Logger log = LoggerFactory.getLogger(FireSeasonConfigServiceImpl.class);
|
||||||
|
|
||||||
|
private final Locale locale = LocaleContextHolder.getLocale();
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FirePointChannelConfigDao channelConfigDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FirePointOrdinaryDao firePointOrdinaryDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FirePointQueryManager firePointQueryManager;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FirePointPushManager firePointPushManager;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysUserDao sysUserDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StreetDao streetDao;
|
||||||
|
|
||||||
|
private FireSeasonConfigDao fireSeasonConfigDao;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String updateFireSeasonConfig() {
|
||||||
|
try {
|
||||||
|
long timeTag1 = System.currentTimeMillis();
|
||||||
|
log.info("开始更新订阅:");
|
||||||
|
LocalDateTime now = LocalDateTime.now();//当前时间,用来判断是否处于防火季
|
||||||
|
List<String> fireSeasonCountyCodeList = new ArrayList<>();//处于防火季的区划编码列表
|
||||||
|
List<FireSeasonConfigEntity> fireSeasonConfigList = fireSeasonConfigDao.findAll();//获取全部防火季配置项
|
||||||
|
for (FireSeasonConfigEntity fireSeasonConfig : fireSeasonConfigList) {
|
||||||
|
String startTime = fireSeasonConfig.getStartTime();//开始时间
|
||||||
|
String endTime = fireSeasonConfig.getEndTime();//结束时间
|
||||||
|
String countyCode = fireSeasonConfig.getCountyCode();//区划编码
|
||||||
|
DateTimeUtils.checkDateTimeFormat(startTime);//校验开始时间格式
|
||||||
|
DateTimeUtils.checkDateTimeFormat(endTime);//校验结束时间格式
|
||||||
|
LocalDateTime oldStartLocalDateTime = DateTimeUtils.formatToLocalDateTime(startTime);//解析原始的开始时间
|
||||||
|
LocalDateTime oldEndLocalDateTime = DateTimeUtils.formatToLocalDateTime(endTime);//解析原始的结束时间
|
||||||
|
LocalDateTime newStartLocalDateTime = oldStartLocalDateTime.plusYears(LocalDateTime.now().getYear() - oldStartLocalDateTime.getYear());//同步到今年的开始时间
|
||||||
|
LocalDateTime newEndLocalDateTime = oldEndLocalDateTime.plusYears(LocalDateTime.now().getYear() - oldEndLocalDateTime.getYear());//同步到今年的结束时间
|
||||||
|
if (newStartLocalDateTime.isBefore(now) && newEndLocalDateTime.isAfter(now)) {
|
||||||
|
fireSeasonCountyCodeList.add(countyCode);//这个区划现在正处于防火季,加入到订阅列表中
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("区划信息列表(收集完毕):" + CountyCodeUtils.obtainLogInfo(fireSeasonConfigList, fireSeasonCountyCodeList));
|
||||||
|
List<String> collectList = CountyCodeUtils.mergeCountyCodeList(fireSeasonCountyCodeList).stream().sorted(String::compareTo).collect(Collectors.toList());
|
||||||
|
System.out.println("区划信息列表(收集、合并、排序完毕):" + CountyCodeUtils.obtainLogInfo(fireSeasonConfigList, collectList));
|
||||||
|
//发送订阅请求
|
||||||
|
String subscribeUrl = "http://118.24.27.47:5060/push-firepoint/ModifySystemMessages";
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("name", "秸秆火系统正式版");
|
||||||
|
map.put("url", "http://118.24.27.47:6801/updateFirePoint");
|
||||||
|
map.put("param", ListUtils.fromStringList(collectList, ","));
|
||||||
|
map.put("typeOfLand", "耕地");
|
||||||
|
String updateResult = sendHttpPostTextPlain(subscribeUrl, new ObjectMapper().writeValueAsString(map));
|
||||||
|
long timeTag3 = System.currentTimeMillis();
|
||||||
|
log.info("更新订阅结束,总耗时:" + ((timeTag3 - timeTag1) / 1000D) + "秒。");
|
||||||
|
return updateResult;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return e.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,7 +3,6 @@ package com.xkrs.helper;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.xkrs.dao.FirePointChannelConfigDao;
|
import com.xkrs.dao.FirePointChannelConfigDao;
|
||||||
import com.xkrs.dao.SysUserDao;
|
import com.xkrs.dao.SysUserDao;
|
||||||
import com.xkrs.model.bean.DataWrapper4;
|
|
||||||
import com.xkrs.model.bean.FirePointChannelConfigBean;
|
import com.xkrs.model.bean.FirePointChannelConfigBean;
|
||||||
import com.xkrs.model.bean.LocalDateTimeRange;
|
import com.xkrs.model.bean.LocalDateTimeRange;
|
||||||
import com.xkrs.model.entity.FirePointChannelConfigEntity;
|
import com.xkrs.model.entity.FirePointChannelConfigEntity;
|
||||||
@ -59,7 +58,7 @@ public class FirePointSubscribeManager {
|
|||||||
}
|
}
|
||||||
//在防火季,就获取全部系统用户
|
//在防火季,就获取全部系统用户
|
||||||
List<String> countyCodeList = obtainCountyCodeList(sysUserDao.findAll());
|
List<String> countyCodeList = obtainCountyCodeList(sysUserDao.findAll());
|
||||||
List<String> mergedCountyCodeList = mergeCountyCodeList(countyCodeList);
|
List<String> mergedCountyCodeList = CountyCodeUtils.mergeCountyCodeList(countyCodeList);
|
||||||
mergedCountyCodeList.sort(Comparator.naturalOrder());
|
mergedCountyCodeList.sort(Comparator.naturalOrder());
|
||||||
return mergedCountyCodeList;
|
return mergedCountyCodeList;
|
||||||
}
|
}
|
||||||
@ -134,71 +133,4 @@ public class FirePointSubscribeManager {
|
|||||||
return countyCodeList;
|
return countyCodeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 合并区划编码
|
|
||||||
* 将获取的全部VIP用户的区划编码,进行合并,去除冗余
|
|
||||||
*/
|
|
||||||
private List<String> mergeCountyCodeList(List<String> countyCodeList) {
|
|
||||||
List<String> mergedCountyCodeList = new ArrayList<>();
|
|
||||||
for (String countyCode : countyCodeList) {
|
|
||||||
DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged = checkMerged(mergedCountyCodeList, countyCode);
|
|
||||||
Boolean wasMerged = checkMerged.getData1();
|
|
||||||
Boolean needReplace = checkMerged.getData2();
|
|
||||||
List<String> oldValueList = checkMerged.getData3();
|
|
||||||
String newValue = checkMerged.getData4();
|
|
||||||
if (!wasMerged) {
|
|
||||||
if (needReplace) {
|
|
||||||
mergedCountyCodeList.removeAll(oldValueList);
|
|
||||||
}
|
|
||||||
mergedCountyCodeList.add(newValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mergedCountyCodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在mergedCountyCodeList中检查传入的countyCode是否被合并过了
|
|
||||||
* DataWrapper4<Boolean, Boolean, String, String>
|
|
||||||
* Boolean 是否被合并了
|
|
||||||
* Boolean 如果1==true则2失效,如果1==false则2表示这个未被合并的数据是否需要替换,如果2==true则将3替换成4,如果2==false则只添加4,忽略3
|
|
||||||
* String 如果1==false并且2==true(这个数据没有被合并,并且它需要替换添加),3是待替换的旧值
|
|
||||||
* String 如果1==false并且2==true(这个数据没有被合并,并且它需要替换添加),4是要替换成的新值;如果1==false并且2==false(这个数据没有被合并,并且它不需要替换添加),4是要添加的
|
|
||||||
*/
|
|
||||||
private DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged(List<String> mergedCountyCodeList, String countyCode) {
|
|
||||||
for (String mergedCountyCode : mergedCountyCodeList) {
|
|
||||||
try {
|
|
||||||
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
|
|
||||||
String cutCountyCode = FirePointCodeUtils.getFormatCutCode(countyCode);
|
|
||||||
if (cutCountyCode.equals(cutMergedCountyCode) || cutCountyCode.startsWith(cutMergedCountyCode)) {
|
|
||||||
return new DataWrapper4<>(Boolean.TRUE, null, null, null);
|
|
||||||
}
|
|
||||||
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
|
|
||||||
List<String> list = obtainRedundantMergedCountyCodeList(mergedCountyCodeList, cutCountyCode);
|
|
||||||
return new DataWrapper4<>(Boolean.FALSE, Boolean.TRUE, list, countyCode);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new DataWrapper4<>(Boolean.FALSE, Boolean.FALSE, null, countyCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 在发现了一个新的、范围更大的区划时,获取这个区划下边已经包含的、冗余的、范围小一点的区划
|
|
||||||
*/
|
|
||||||
private List<String> obtainRedundantMergedCountyCodeList(List<String> mergedCountyCodeList, String cutCountyCode) {
|
|
||||||
List<String> redundantMergedCountyCodeList = new ArrayList<>();
|
|
||||||
for (String mergedCountyCode : mergedCountyCodeList) {
|
|
||||||
try {
|
|
||||||
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
|
|
||||||
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
|
|
||||||
redundantMergedCountyCodeList.add(mergedCountyCode);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return redundantMergedCountyCodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package com.xkrs.service;
|
|
||||||
|
|
||||||
public interface FireSeasonConfigService {
|
|
||||||
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package com.xkrs.service.impl;
|
|
||||||
|
|
||||||
import com.xkrs.dao.FirePointChannelConfigDao;
|
|
||||||
import com.xkrs.dao.FirePointOrdinaryDao;
|
|
||||||
import com.xkrs.dao.StreetDao;
|
|
||||||
import com.xkrs.dao.SysUserDao;
|
|
||||||
import com.xkrs.helper.FirePointPushManager;
|
|
||||||
import com.xkrs.helper.FirePointQueryManager;
|
|
||||||
import com.xkrs.service.FireSeasonConfigService;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.context.i18n.LocaleContextHolder;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class FireSeasonConfigServiceImpl implements FireSeasonConfigService {
|
|
||||||
|
|
||||||
public static Logger log = LoggerFactory.getLogger(FireSeasonConfigServiceImpl.class);
|
|
||||||
|
|
||||||
private final Locale locale = LocaleContextHolder.getLocale();
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FirePointChannelConfigDao channelConfigDao;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FirePointOrdinaryDao firePointOrdinaryDao;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FirePointQueryManager firePointQueryManager;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FirePointPushManager firePointPushManager;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private SysUserDao sysUserDao;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private StreetDao streetDao;
|
|
||||||
|
|
||||||
}
|
|
105
src/main/java/com/xkrs/utilsnew/CountyCodeUtils.java
Normal file
105
src/main/java/com/xkrs/utilsnew/CountyCodeUtils.java
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
package com.xkrs.utilsnew;
|
||||||
|
|
||||||
|
import com.xkrs.fireseason.FireSeasonConfigEntity;
|
||||||
|
import com.xkrs.model.bean.DataWrapper4;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CountyCodeUtils {
|
||||||
|
|
||||||
|
private CountyCodeUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并区划编码
|
||||||
|
* 将获取的全部VIP用户的区划编码,进行合并,去除冗余
|
||||||
|
*/
|
||||||
|
public static List<String> mergeCountyCodeList(List<String> countyCodeList) {
|
||||||
|
List<String> mergedCountyCodeList = new ArrayList<>();
|
||||||
|
for (String countyCode : countyCodeList) {
|
||||||
|
DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged = checkMerged(mergedCountyCodeList, countyCode);
|
||||||
|
Boolean wasMerged = checkMerged.getData1();
|
||||||
|
Boolean needReplace = checkMerged.getData2();
|
||||||
|
List<String> oldValueList = checkMerged.getData3();
|
||||||
|
String newValue = checkMerged.getData4();
|
||||||
|
if (!wasMerged) {
|
||||||
|
if (needReplace) {
|
||||||
|
mergedCountyCodeList.removeAll(oldValueList);
|
||||||
|
}
|
||||||
|
mergedCountyCodeList.add(newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mergedCountyCodeList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在mergedCountyCodeList中检查传入的countyCode是否被合并过了
|
||||||
|
* DataWrapper4<Boolean, Boolean, String, String>
|
||||||
|
* Boolean 是否被合并了
|
||||||
|
* Boolean 如果1==true则2失效,如果1==false则2表示这个未被合并的数据是否需要替换,如果2==true则将3替换成4,如果2==false则只添加4,忽略3
|
||||||
|
* String 如果1==false并且2==true(这个数据没有被合并,并且它需要替换添加),3是待替换的旧值
|
||||||
|
* String 如果1==false并且2==true(这个数据没有被合并,并且它需要替换添加),4是要替换成的新值;如果1==false并且2==false(这个数据没有被合并,并且它不需要替换添加),4是要添加的
|
||||||
|
*/
|
||||||
|
private static DataWrapper4<Boolean, Boolean, List<String>, String> checkMerged(List<String> mergedCountyCodeList, String countyCode) {
|
||||||
|
for (String mergedCountyCode : mergedCountyCodeList) {
|
||||||
|
try {
|
||||||
|
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
|
||||||
|
String cutCountyCode = FirePointCodeUtils.getFormatCutCode(countyCode);
|
||||||
|
if (cutCountyCode.equals(cutMergedCountyCode) || cutCountyCode.startsWith(cutMergedCountyCode)) {
|
||||||
|
return new DataWrapper4<>(Boolean.TRUE, null, null, null);
|
||||||
|
}
|
||||||
|
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
|
||||||
|
List<String> list = obtainRedundantMergedCountyCodeList(mergedCountyCodeList, cutCountyCode);
|
||||||
|
return new DataWrapper4<>(Boolean.FALSE, Boolean.TRUE, list, countyCode);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new DataWrapper4<>(Boolean.FALSE, Boolean.FALSE, null, countyCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在发现了一个新的、范围更大的区划时,获取这个区划下边已经包含的、冗余的、范围小一点的区划
|
||||||
|
*/
|
||||||
|
private static List<String> obtainRedundantMergedCountyCodeList(List<String> mergedCountyCodeList, String cutCountyCode) {
|
||||||
|
List<String> redundantMergedCountyCodeList = new ArrayList<>();
|
||||||
|
for (String mergedCountyCode : mergedCountyCodeList) {
|
||||||
|
try {
|
||||||
|
String cutMergedCountyCode = FirePointCodeUtils.getFormatCutCode(mergedCountyCode);
|
||||||
|
if (cutMergedCountyCode.startsWith(cutCountyCode)) {
|
||||||
|
redundantMergedCountyCodeList.add(mergedCountyCode);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return redundantMergedCountyCodeList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印countyCodeList的信息
|
||||||
|
*/
|
||||||
|
public static String obtainLogInfo(List<FireSeasonConfigEntity> fireSeasonConfigList, List<String> countyCodeList) {
|
||||||
|
try {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
for (String countyCode : countyCodeList) {
|
||||||
|
list.add(countyCode + "(" + getCountyName(fireSeasonConfigList, countyCode) + ")");
|
||||||
|
}
|
||||||
|
return "[" + ListUtils.fromStringList(list, ",") + "]";
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getCountyName(List<FireSeasonConfigEntity> fireSeasonConfigList, String countyCode) {
|
||||||
|
for (FireSeasonConfigEntity fireSeasonConfig : fireSeasonConfigList) {
|
||||||
|
if (fireSeasonConfig.getCountyCode().equals(countyCode)) {
|
||||||
|
return fireSeasonConfig.getCountyName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,12 @@
|
|||||||
package com.xkrs.utilsnew;
|
package com.xkrs.utilsnew;
|
||||||
|
|
||||||
|
import org.apache.hc.core5.util.TextUtils;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class DateTimeUtils {
|
public class DateTimeUtils {
|
||||||
|
|
||||||
@ -13,6 +17,11 @@ public class DateTimeUtils {
|
|||||||
return DATE_TIME_FORMATTER_1.format(localDateTime);
|
return DATE_TIME_FORMATTER_1.format(localDateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LocalDateTime formatToLocalDateTime(String formattedLocalDateTime) {
|
||||||
|
checkDateTimeFormat(formattedLocalDateTime);
|
||||||
|
return LocalDateTime.parse(formattedLocalDateTime, DateTimeUtils.DATE_TIME_FORMATTER_1);
|
||||||
|
}
|
||||||
|
|
||||||
public static LocalDateTime stringToLocalDateTime(String localDateTimeString) {
|
public static LocalDateTime stringToLocalDateTime(String localDateTimeString) {
|
||||||
try {
|
try {
|
||||||
return LocalDateTime.parse(localDateTimeString, DATE_TIME_FORMATTER_1);
|
return LocalDateTime.parse(localDateTimeString, DATE_TIME_FORMATTER_1);
|
||||||
@ -21,4 +30,33 @@ public class DateTimeUtils {
|
|||||||
return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
|
return LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断时间格式 格式必须为 "yyyy-MM-dd HH:mm:ss"
|
||||||
|
*/
|
||||||
|
public static boolean isValidDateTime(String formattedLocalDateTime) {
|
||||||
|
int validLength = 19;
|
||||||
|
if (TextUtils.isEmpty(formattedLocalDateTime) || formattedLocalDateTime.length() != validLength) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String regex = "^([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
Matcher matcher = pattern.matcher(formattedLocalDateTime);
|
||||||
|
return matcher.matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void checkDateTimeFormat(String formattedLocalDateTime) {
|
||||||
|
boolean valid = isValidDateTime(formattedLocalDateTime);
|
||||||
|
if (!valid) {
|
||||||
|
throw new RuntimeException("日期时间格式校验失败:(" + formattedLocalDateTime + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
boolean validDateTime = isValidDateTime("2222-01-21 09:09:59");
|
||||||
|
System.out.println("validDateTime = " + validDateTime);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
package com.xkrs.utilsold;
|
package com.xkrs.utilsold;
|
||||||
|
|
||||||
import org.apache.hc.core5.util.TextUtils;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日期时间工具
|
* 日期时间工具
|
||||||
@ -76,25 +72,4 @@ public class DateTimeUtil {
|
|||||||
return COMMON_FORMATTER_DATETIME.format(LocalDateTime.now());
|
return COMMON_FORMATTER_DATETIME.format(LocalDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss"
|
|
||||||
*/
|
|
||||||
public static boolean isValidDateTime(String formattedDateTime) {
|
|
||||||
int validLength = 19;
|
|
||||||
if (TextUtils.isEmpty(formattedDateTime) || formattedDateTime.length() != validLength) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String regex = "^([1-2][0-9][0-9][0-9]-[0-1]{0,1}[0-9]-[0-3]{0,1}[0-9])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";
|
|
||||||
Pattern pattern = Pattern.compile(regex);
|
|
||||||
Matcher matcher = pattern.matcher(formattedDateTime);
|
|
||||||
return matcher.matches();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
// boolean validDateTime = isValidDateTime("2222-01-21 19:59:59");
|
|
||||||
// System.out.println("validDateTime = " + validDateTime);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user