****最近项目上线,基于第一次带项目,并且接上个技术组长的坑
帖子会一直更新,附上遇到的一些问题
mysql时间类型,String类型入库
{
"documentName": "测试002",
"documentSymbol": "33",
"documentType": "02",
"dispatchTime": "2020-05-25",
"batch": "444",
"dispatchUnit": "贵州省能源局",
"receiptUnit": "",
"dispatchregion": "贵州省",
"starttime": "0000-00-00 ",
"endtime": "0000-00-00 ",
"isrelease": "0",
"market": "05",
"createperson": "朱刚毅",
"createtime": "2020-05-23 15:39:38.0",
"fileID": "eb3d5a3d39b14ee88cb598d34499e60b",
"participantType": null,
"entrytime": "0000-00-00 00:00:00",
"fileName": null,
"orderType": null
}
必须统一的是入库约定的datetime格式 “yyyy-mm-dd hh:mm:ss”,date格式"yyyy-mm-dd" ,数据库datetime,date支持字符串类型的存储
必须要说的一个坑
"createtime": "2020-05-23 15:39:38.0"
数据库查询不做转化的格式
这个毫秒级别会影响入库所以如下字符串格式化
可以用截取的方式保持与"yyyy-mm-dd hh:mm:ss"或者"yyyy-mm-dd"一直
"yyyy-mm-dd hh:mm:ss 19位datetime类型"
if (StringUtils.isNotEmpty(enterTime)){
int i=enterTime.length()-19;
if (enterTime.length()>19){
enterTime=enterTime.substring(0,enterTime.length()-i);
mmSellElecParticipantRecorModel.setEnterTime(enterTime);
}
}
去掉格式化后面的位数入库就能保持"yyyy-mm-dd hh:mm:ss"
"yyyy-mm-dd"date类型
if (StringUtils.isNotEmpty(enterTime)){
int i=enterTime.length()-10;
if (enterTime.length()>10){
enterTime=enterTime.substring(0,enterTime.length()-i);
mmSellElecParticipantRecorModel.setEnterTime(enterTime);
}
}
时间工具类
实体类中registdate是localDateTime类型,JDK1.8以后localDateTime线程安全
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime.parse(registdate,formatter)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd ");
LocalDateTime.parse(registdate,formatter)
附上
String dateString="2020-12-20 12:05:22.0";
String dateString1="2020-12-20";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate parse = LocalDate.parse(dateString1,formatter);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
int i=dateString.length()-19;
if (dateString.length()>19){
dateString= dateString.substring(0,dateString.length()-i);
}
LocalDateTime parse1 = LocalDateTime.parse(dateString, formatter1);
dateDto.setD1(parse);
dateDto.setD2(parse1);