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.
udi-wms-java/src/main/java/com/glxp/api/idc/utils/IDCUtils.java

178 lines
5.9 KiB
Java

package com.glxp.api.idc.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* @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;
}
public static void createDirectory(String directory) {
String dir = directory;
String path ="";
String[] breakChar ={"\\","/"};
for(String bk:breakChar) {
while(dir.indexOf(bk)>-1) {
path+=dir.substring(0,dir.indexOf(bk))+"\\";
File file = new File(path);
if (!(new File(path)).exists()) {
(new File(path)).mkdir();
}
if (dir.length()>dir.indexOf(bk)) {
dir = dir.substring(dir.indexOf(bk) +1);
} else {
dir = "";
}
}
}
}
public static String post(String url, Map<String, Object> params) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "");
if (params != null)
body = RequestBody.create(mediaType, JSON.toJSONString(params));
Request request = new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
String result = "";
try {
Response response = client.newCall(request).execute();
result = response.body().string();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void writeFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
}
}