You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.9 KiB
Java
113 lines
3.9 KiB
Java
package com.glxp.api.idc.utils;
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @author chenqf
|
|
*/
|
|
public class IDCUtils {
|
|
private static final Logger logger = LoggerFactory.getLogger(IDCUtils.class);
|
|
private static String aliasNameChars = "_hijklmnopqrstuvwxyzabcdefg9876543210";
|
|
private static String realNamechars = "abcdefghijklmnopqrstuvwxyz_0123456789";
|
|
private final static String MONTH_EN = "JanFebMarAprMayJunJulAugSepOctNovDec";
|
|
|
|
//判断是否json字符串
|
|
public static boolean isJson(String json) {
|
|
if(json!=null&&json.contains("{")&&json.contains("}")&&json.contains("\""))
|
|
return true;
|
|
return false;
|
|
}
|
|
public static Date parseDate(String str) {
|
|
return parseDate(str,"yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
public static Date parseDate(String str,String fmt) {
|
|
String dateStr=str.replace("?", " ");
|
|
SimpleDateFormat df = new SimpleDateFormat(fmt);
|
|
if(str!=null&&!str.contains("-")&&str.length()>7&&StringUtils.isNumeric(str.substring(0, 4))&&
|
|
Long.valueOf(str.substring(0,4))>1899&&Long.valueOf(str.substring(0,4))<2199&&
|
|
StringUtils.isNumeric(str.substring(4, 6))&&StringUtils.isNumeric(str.substring(6, 8))&&
|
|
(!str.contains(" ")||(str.contains(" ")&&str.indexOf(" ")>7))) {
|
|
dateStr = str.substring(0,4)+"-"+str.substring(4,6)+"-"+str.substring(6,8);
|
|
dateStr+=" ";
|
|
if(str.contains(" ")) {
|
|
if(str.length()>9) {
|
|
dateStr+= str.substring(9,11);
|
|
if(str.length()>11) {
|
|
dateStr+=":"+str.substring(11,13);
|
|
if(str.length()>13) {
|
|
dateStr+=":"+str.substring(13,15);
|
|
}
|
|
} else {
|
|
dateStr+=":00:00";
|
|
}
|
|
} else {
|
|
dateStr+="00:00:00";
|
|
}
|
|
} else {
|
|
if(str.length()>8) {
|
|
dateStr+= str.substring(8,10);
|
|
if(str.length()>10) {
|
|
dateStr+=":"+str.substring(10,12);
|
|
if(str.length()>13) {
|
|
dateStr+=":"+str.substring(12,14);
|
|
} if(str.length()>12) {
|
|
dateStr+=":"+str.substring(12,13)+"0";
|
|
}
|
|
} else {
|
|
dateStr+=":00:00";
|
|
}
|
|
} else {
|
|
dateStr+="00:00:00";
|
|
}
|
|
}
|
|
} else if(str!=null&&str.contains("CST")) {
|
|
//Wed Feb 16 15:35:31 CST 2022
|
|
int index = str.indexOf("CST");
|
|
int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3;
|
|
dateStr = str.substring(index+4)+"-";
|
|
if(month<10)
|
|
dateStr+="0";
|
|
dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1);
|
|
} else if(str!=null&&str.contains("GMT")) {
|
|
int index = str.indexOf("GMT");
|
|
int month = (MONTH_EN.indexOf(str.substring(index -16,index -13))+3)/3;
|
|
if(str.contains("GMT+")) {
|
|
dateStr = str.substring(index+10)+"-";
|
|
} else {
|
|
dateStr = str.substring(index+4)+"-";
|
|
}
|
|
if(month<10)
|
|
dateStr+="0";
|
|
dateStr+= month+"-"+str.substring(index -12,index -10)+" "+str.substring(index -9,index -1);
|
|
} else if (str!=null&&str.equals("-30609820800000")) {
|
|
dateStr="1000-01-01 00:01:01";
|
|
} else if (str!=null&&str.length()==4&&StringUtils.isNumeric(str)&&Integer.valueOf(str)>999) {
|
|
dateStr=str+"-01-01 00:01:01";
|
|
} else if (str!=null&&str.length()==6&&StringUtils.isNumeric(str)) {
|
|
dateStr=str.substring(0, 4)+"-"+str.substring(4)+"-01 00:01:01";
|
|
} else if(str!=null&&StringUtils.isNumeric(str)&&str.length()>8&&(Long.valueOf(str.substring(0,4))<1899||Long.valueOf(str.substring(0,4))>2200)) {
|
|
dateStr=df.format(new Date(Long.valueOf(str)));
|
|
}
|
|
dateStr = dateStr.replace("T", " ");
|
|
|
|
Date date = null;
|
|
try {
|
|
date = df.parse(dateStr);
|
|
|
|
} catch (ParseException e) {
|
|
logger.error(e.getMessage());
|
|
}
|
|
return date;
|
|
}
|
|
|
|
}
|