项目中有很多需要工具类的地方,在这里总结一下,下一次直接用, 节约时间!
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.codehaus.jackson.map.ObjectMapper;
import com.alibaba.fastjson.JSON;
/**
* 经纬度校验
* @param longitude
* @param latitude
* @return
*/
public static boolean checkItude(String longitude,String latitude){
String reglo = "((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,6}))|((?:180)\\.([0]{0,6}))";
String regla = "((?:[0-9]|[1-8][0-9])\\.([0-9]{0,6}))|((?:90)\\.([0]{0,6}))";
longitude = longitude.trim();
latitude = latitude.trim();
return longitude.matches(reglo)==true?latitude.matches(regla):false;
}
/**
* Description 检查一个对象是否为空
*/
@SuppressWarnings("rawtypes")
public static boolean isBlank(Object obj) {
if (obj == null)
return true;
if (obj instanceof String)
return (obj.toString().trim()).equals("");
if (obj instanceof CharSequence)
return ((CharSequence) obj).length() == 0;
if (obj instanceof Collection)
return ((Collection) obj).isEmpty();
if (obj instanceof Map)
return ((Map) obj).isEmpty();
if (obj instanceof Object[]) {
Object[] object = (Object[]) obj;
boolean empty = true;
for (int i = 0; i < object.length; i++)
if (!isBlank(object[i])) {
empty = false;
break;
}
return empty;
}
return false;
}
/**
* Description 检查一个对象不为空
*/
public static boolean isNotBlank(Object obj){
return (!isBlank(obj));
}
/**
* Description 获取当前日期 yyyy-MM-dd HH:mm:ss
*/
public static String getToday() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime = formatter.format(cal.getTime());
return mDateTime;
}
/**
* Description 获取当前日期 yyyy-MM-dd
*/
public static String getToday2() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String mDateTime = formatter.format(cal.getTime());
return mDateTime;
}
/**
* Description 获取当前日期 yyyyMMddHHmmss
*/
public static String getToday3() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String mDateTime = formatter.format(cal.getTime());
return mDateTime;
}
/**
* Description 日期转换:Date转String
* @param date yyyy-MM-dd
*/
public static String DateToString(Date date) {
if(MyUtils.isBlank(date)){
try {
return "";
} catch (Exception e) {
e.printStackTrace();
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String stringDte = sdf.format(date);
return stringDte;
}
/**
* Description 日期转换:Date转String
* @param date yyyy-MM-dd HH:mm:ss
*/
public static String DateToString2(Date date) {
if(MyUtils.isBlank(date)){
try {
return "";
} catch (Exception e) {
e.printStackTrace();
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stringDte = sdf.format(date);
return stringDte;
}
/**
* Description String转化为Date(yyyy-MM-dd)
*/
public static Date StrToDate(String str) throws ParseException{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.parse(str);
}
/**
* Description String转化为Date(yyyy-MM)
*/
public static Date StrToDate2(String str) throws ParseException{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
return formatter.parse(str);
}
/**
* Description String转化为Date(yyyy-MM-dd HH:mm:ss)
*/
public static Date StrToDate3(String str) throws ParseException{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.parse(str);
}
/**
* Description 将实体POJO转化为JSON
*/
public static String objToJson(Object obj) {
ObjectMapper mapper=new ObjectMapper();
Writer writer=new StringWriter();
String json="";
try {
mapper.writeValue(writer, obj);
json=writer.toString();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
/**
* Description List转化为Json
*/
@SuppressWarnings("rawtypes")
public static String ListToJson(List list){
return JSON.toJSON(list).toString();
}
/**
* Description Map转JSON
*/
public static String MapToJson(Map<String,Object> map){
JSONObject jsonObj = JSONObject.fromObject(map);
return jsonObj.toString();
}
/**
* Description 将json转化为实体类
*/
public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
Object object = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
object = objectMapper.readValue(jsonStr,obj);
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
/**
* Description 读取配置文件
*/
public static Properties getProperties(String str){
Properties pro = new Properties();
InputStream in = MyUtils.class.getClassLoader().getResourceAsStream(str);
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pro;
}
/**
* Description apache正向代理
*/
public static Proxy getProxy(){
Properties prop = MyUtils.getProperties("supplier.properties");
Random r = new Random();
int nextInt = r.nextInt(10);
Integer prodPort= Integer.valueOf(prop.getProperty("prodPort"));
InetSocketAddress addr = new InetSocketAddress(prop.getProperty("prodIP1"), prodPort);
if (nextInt % 2 == 0) {
addr = new InetSocketAddress(prop.getProperty("prodIP1"), prodPort);
}
if (nextInt % 2 == 1) {
addr = new InetSocketAddress(prop.getProperty("prodIP2"), prodPort);
}
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
return proxy;
}
/**
* Description 参数字符串检查 返回true表示存在sql注入非法字符
* @param paramStr 参数字符
*/
public static boolean numCheck(String paramStr){
String injectStr = "^[0-9]*$";
if(paramStr.matches(injectStr)){
return false;
}
return true;
}
/**
* Description 防sql注入特殊字符转义
* @param str
*/
public static String escapeSql(String str){
return StringEscapeUtils.escapeSql(str);
}
/**
* Description 清除特殊字符
*/
public static String StringFilter(String str) {
// 清除掉所有特殊字符
String regEx = "[%*()+=|'',//[//].<>/?—]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}