/**
* 根据输入的字符串类型的日期时间,转换为标准时间
* # 日期类型返回浏览器相差8小时 返回浏览器端的数据会被spring-boot默认转换为json,而框架内部默认的时区配置为GMT
* 配置文件添加 spring.jackson.time-zone=GMT+8
* @param dateTime 字符串类型日期时间
* @return 标准时间
*/
public static Date stringToDateTime(String dateTime) throws ParseException {
if (dateTime==null||dateTime.length()==0){
return null;
}
String reg = "\\d{2}-[1-9]-[1-9]";
if (dateTime.matches(reg)){
return new SimpleDateFormat("yy-M-d").parse(dateTime);
}
String reg1 = "\\d{2}-[1-9]-[0-3][0-9]";
if (dateTime.matches(reg1)){
return new SimpleDateFormat("yy-M-dd").parse(dateTime);
}
String reg2 = "\\d{2}-[012][0-9]-[0-3][0-9]";
if (dateTime.matches(reg2)){
return new SimpleDateFormat("yy-MM-dd").parse(dateTime);
}
String reg3 = "\\d{2}-[012][0-9]-[1-9]";
if (dateTime.matches(reg3)){
return new SimpleDateFormat("yy-MM-d").parse(dateTime);
}
String reg4 = "\\d{4}-[1-9]-[1-9]";
if (dateTime.matches(reg4)){
return new SimpleDateFormat("yyyy-M-d").parse(dateTime);
}
String reg5 = "\\d{4}-[1-9]-[0-3][0-9]";
if (dateTime.matches(reg5)){
return new SimpleDateFormat("yyyy-M-dd").parse(dateTime);
}
String reg6 = "\\d{4}-[012][0-9]-[0-3][0-9]";
if (dateTime.matches(reg6)){
return new SimpleDateFormat("yyyy-MM-dd").parse(dateTime);
}
String reg7 = "\\d{4}-[012][0-9]-[1-9]";
if (dateTime.matches(reg7)){
return new SimpleDateFormat("yyyy-MM-d").parse(dateTime);
}
String reg8 = "\\d{2}/[1-9]/[1-9]";
if (dateTime.matches(reg8)){
return new SimpleDateFormat("yy/M/d").parse(dateTime);
}
String reg9 = "\\d{2}/[1-9]/[0-3][0-9]";
if (dateTime.matches(reg9)){
return new SimpleDateFormat("yy/M/dd").parse(dateTime);
}
String reg10 = "\\d{2}/[012][0-9]/[0-3][0-9]";
if (dateTime.matches(reg10)){
return new SimpleDateFormat("yy/MM/dd").parse(dateTime);
}
String reg11 = "\\d{2}/[012][0-9]/[1-9]";
if (dateTime.matches(reg11)){
return new SimpleDateFormat("yy/MM/d").parse(dateTime);
}
String reg12 = "\\d{4}/[1-9]/[1-9]";
if (dateTime.matches(reg12)){
return new SimpleDateFormat("yyyy/M/d").parse(dateTime);
}
String reg13 = "\\d{4}/[1-9]/[0-3][0-9]";
if (dateTime.matches(reg13)){
return new SimpleDateFormat("yyyy/M/dd").parse(dateTime);
}
String reg14 = "\\d{4}/[012][0-9]/[0-3][0-9]";
if (dateTime.matches(reg14)){
return new SimpleDateFormat("yyyy/MM/dd").parse(dateTime);
}
String reg15 = "\\d{4}/[012][0-9]/[1-9]";
if (dateTime.matches(reg15)){
return new SimpleDateFormat("yyyy/MM/d").parse(dateTime);
}
String tReg = "\\d{2}-[1-9]-[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg)){
return new SimpleDateFormat("yy-M-d HH:mm").parse(dateTime);
}
String tReg1 = "\\d{2}-[1-9]-[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg1)){
return new SimpleDateFormat("yy-M-dd HH:mm").parse(dateTime);
}
String tReg2 = "\\d{2}-[012][0-9]-[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg2)){
return new SimpleDateFormat("yy-MM-dd HH:mm").parse(dateTime);
}
String tReg3 = "\\d{2}-[012][0-9]-[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg3)){
return new SimpleDateFormat("yy-MM-d HH:mm").parse(dateTime);
}
String tReg4 = "\\d{4}-[1-9]-[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg4)){
return new SimpleDateFormat("yyyy-M-d HH:mm").parse(dateTime);
}
String tReg5 = "\\d{4}-[1-9]-[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg5)){
return new SimpleDateFormat("yyyy-M-dd HH:mm").parse(dateTime);
}
String tReg6 = "\\d{4}-[012][0-9]-[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg6)){
return new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(dateTime);
}
String tReg7 = "\\d{4}-[012][0-9]-[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg7)){
return new SimpleDateFormat("yyyy-MM-d HH:mm").parse(dateTime);
}
String tReg8 = "\\d{2}/[1-9]/[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg8)){
return new SimpleDateFormat("yy/M/d HH:mm").parse(dateTime);
}
String tReg9 = "\\d{2}/[1-9]/[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg9)){
return new SimpleDateFormat("yy/M/dd HH:mm").parse(dateTime);
}
String tReg10 = "\\d{2}/[012][0-9]/[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg10)){
return new SimpleDateFormat("yy/MM/dd HH:mm").parse(dateTime);
}
String tReg11 = "\\d{2}/[012][0-9]/[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg11)){
return new SimpleDateFormat("yy/MM/d HH:mm").parse(dateTime);
}
String tReg12 = "\\d{4}/[1-9]/[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg12)){
return new SimpleDateFormat("yyyy/M/d HH:mm").parse(dateTime);
}
String tReg13 = "\\d{4}/[1-9]/[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg13)){
return new SimpleDateFormat("yyyy/M/dd HH:mm").parse(dateTime);
}
String tReg14 = "\\d{4}/[012][0-9]/[0-3][0-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg14)){
return new SimpleDateFormat("yyyy/MM/dd HH:mm").parse(dateTime);
}
String tReg15 = "\\d{4}/[012][0-9]/[1-9] \\d{2}:\\d{2}";
if (dateTime.matches(tReg15)){
return new SimpleDateFormat("yyyy/MM/d HH:mm").parse(dateTime);
}
String tdReg = "\\d{2}-[1-9]-[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg)){
return new SimpleDateFormat("yy-M-d HH:mm:ss").parse(dateTime);
}
String tdReg1 = "\\d{2}-[1-9]-[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg1)){
return new SimpleDateFormat("yy-M-dd HH:mm:ss").parse(dateTime);
}
String tdReg2 = "\\d{2}-[012][0-9]-[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg2)){
return new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(dateTime);
}
String tdReg3 = "\\d{2}-[012][0-9]-[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg3)){
return new SimpleDateFormat("yy-MM-d HH:mm:ss").parse(dateTime);
}
String tdReg4 = "\\d{4}-[1-9]-[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg4)){
return new SimpleDateFormat("yyyy-M-d HH:mm:ss").parse(dateTime);
}
String tdReg5 = "\\d{4}-[1-9]-[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg5)){
return new SimpleDateFormat("yyyy-M-dd HH:mm:ss").parse(dateTime);
}
String tdReg6 = "\\d{4}-[012][0-9]-[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg6)){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime);
}
String tdReg7 = "\\d{4}-[012][0-9]-[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg7)){
return new SimpleDateFormat("yyyy-MM-d HH:mm:ss").parse(dateTime);
}
String tdReg8 = "\\d{2}/[1-9]/[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg8)){
return new SimpleDateFormat("yy/M/d HH:mm:ss").parse(dateTime);
}
String tdReg9 = "\\d{2}/[1-9]/[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg9)){
return new SimpleDateFormat("yy/M/dd HH:mm:ss").parse(dateTime);
}
String tdReg10 = "\\d{2}/[012][0-9]/[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg10)){
return new SimpleDateFormat("yy/MM/dd HH:mm:ss").parse(dateTime);
}
String tdReg11 = "\\d{2}/[012][0-9]/[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg11)){
return new SimpleDateFormat("yy/MM/d HH:mm:ss").parse(dateTime);
}
String tdReg12 = "\\d{4}/[1-9]/[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg12)){
return new SimpleDateFormat("yyyy/M/d HH:mm:ss").parse(dateTime);
}
String tdReg13 = "\\d{4}/[1-9]/[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg13)){
return new SimpleDateFormat("yyyy/M/dd HH:mm:ss").parse(dateTime);
}
String tdReg14 = "\\d{4}/[012][0-9]/[0-3][0-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg14)){
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(dateTime);
}
String tdReg15 = "\\d{4}/[012][0-9]/[1-9] \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(tdReg15)){
return new SimpleDateFormat("yyyy/MM/d HH:mm:ss").parse(dateTime);
}
String nReg = "\\d{2}\\d{2}\\d{2}";
if (dateTime.matches(nReg)){
return new SimpleDateFormat("yyMMdd").parse(dateTime);
}
String nReg1 = "\\d{4}\\d{2}\\d{2}";
if (dateTime.matches(nReg1)){
return new SimpleDateFormat("yyyyMMdd").parse(dateTime);
}
String nReg2 = "\\d{2}\\d{2}\\d{2} \\d{2}:\\d{2}";
if (dateTime.matches(nReg2)){
return new SimpleDateFormat("yyMMdd HH:mm").parse(dateTime);
}
String nReg3 = "\\d{2}\\d{2}\\d{2} \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(nReg3)){
return new SimpleDateFormat("yyMMdd HH:mm:ss").parse(dateTime);
}
String nReg4 = "\\d{4}\\d{2}\\d{2} \\d{2}:\\d{2}";
if (dateTime.matches(nReg4)){
return new SimpleDateFormat("yyyyMMdd HH:mm").parse(dateTime);
}
String nReg5 = "\\d{4}\\d{2}\\d{2} \\d{2}:\\d{2}:\\d{2}";
if (dateTime.matches(nReg5)){
return new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(dateTime);
}
String nReg6 = "\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}";
if (dateTime.matches(nReg6)){
return new SimpleDateFormat("yyMMddHHmm").parse(dateTime);
}
String nReg7 = "\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}";
if (dateTime.matches(nReg7)){
return new SimpleDateFormat("yyMMddHHmmss").parse(dateTime);
}
String nReg8 = "\\d{4}\\d{2}\\d{2}\\d{2}\\d{2}";
if (dateTime.matches(nReg8)){
return new SimpleDateFormat("yyyyMMddHHmm").parse(dateTime);
}
String nReg9 = "\\d{4}\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}";
if (dateTime.matches(nReg9)){
return new SimpleDateFormat("yyyyMMddHHmmss").parse(dateTime);
}
return null;
根据输入的字符串类型的日期时间,转换为标准时间(兼容多种格式)
最新推荐文章于 2024-02-05 11:37:18 发布