1254 lines
36 KiB
Java
1254 lines
36 KiB
Java
package sc545.pay.utils;
|
||
|
||
import java.awt.image.BufferedImage;
|
||
import java.io.*;
|
||
import java.math.BigDecimal;
|
||
import java.net.URL;
|
||
import java.net.URLConnection;
|
||
import java.text.DecimalFormat;
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.ArrayList;
|
||
import java.util.Calendar;
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
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 javax.imageio.ImageIO;
|
||
import javax.servlet.http.Cookie;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
|
||
import org.apache.commons.lang.StringUtils;
|
||
import org.apache.commons.lang.time.DateUtils;
|
||
|
||
public class Utils {
|
||
|
||
public static void main(String[] args) {
|
||
getWeekNum();
|
||
}
|
||
|
||
/**
|
||
* 生成范围内的随机数
|
||
*
|
||
* @param min
|
||
* 最小值(包含)
|
||
* @param max
|
||
* 最大值(不包含)
|
||
* @return
|
||
*/
|
||
public static int randomNum(int min, int max) {
|
||
int num = new Random().nextInt(max - min) + min;
|
||
return num;
|
||
}
|
||
|
||
/**
|
||
* 生成范围内的随机小数
|
||
*
|
||
* @param min
|
||
* 最小值(包含)
|
||
* @param max
|
||
* 最大值(不包含)
|
||
* @param w
|
||
* 小数点后保留位数
|
||
* @return
|
||
*/
|
||
public static Double randomNumForDouble(int min, int max, int w) {
|
||
|
||
BigDecimal db = new BigDecimal(Math.random() * (max - min) + min);
|
||
Double num = db.setScale(w, BigDecimal.ROUND_HALF_UP).doubleValue();
|
||
return num;
|
||
}
|
||
|
||
/**
|
||
* 时间转字符串,格式:yyyy-MM-dd
|
||
*
|
||
* @param d null为当前时间
|
||
*
|
||
* @return
|
||
*/
|
||
public static String date2String(Date d) {
|
||
if(d==null) d=new Date();
|
||
Date date = d;
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
String s = sdf.format(date);
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 时间转字符串
|
||
*
|
||
*
|
||
* @param d null为当前时间
|
||
*
|
||
* @param fm null为"yyyy-MM-dd HH:mm:ss"
|
||
*
|
||
* @return
|
||
*/
|
||
public static String date2String(Date d, String fm) {
|
||
if(d==null) d=new Date();
|
||
Date date = d;
|
||
if (fm == null)
|
||
fm = "yyyy-MM-dd HH:mm:ss";
|
||
SimpleDateFormat sdf = new SimpleDateFormat(fm);
|
||
String s = sdf.format(date);
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 字符串转时间,格式 yyyy-MM-dd
|
||
*
|
||
* @param s
|
||
* 时间字符串
|
||
* @return
|
||
*/
|
||
public static Date string2Date(String s) {
|
||
try {
|
||
String string = s;
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
Date date = sdf.parse(string);
|
||
return date;
|
||
} catch (Exception e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 字符串转时间<br>
|
||
* fm为null时默认"yyyy-MM-dd HH:mm:ss"
|
||
*
|
||
* @param s
|
||
* 时间字符串
|
||
* @param fm
|
||
* 格式
|
||
* @return
|
||
*/
|
||
public static Date string2Date(String s, String fm) {
|
||
try {
|
||
String string = s;
|
||
if (fm == null)
|
||
fm = "yyyy-MM-dd HH:mm:ss";
|
||
SimpleDateFormat sdf = new SimpleDateFormat(fm);
|
||
Date date = sdf.parse(string);
|
||
return date;
|
||
} catch (Exception e) {
|
||
// TODO Auto-generated catch block
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 时间加减法
|
||
*
|
||
* @param d
|
||
* 时间
|
||
* @param day
|
||
* 要操作的天数,正为加,负为减
|
||
* @return
|
||
*/
|
||
public static Date dateadd(Date d, int day) {
|
||
if(d==null) d=new Date();
|
||
Calendar yourd = Calendar.getInstance();
|
||
yourd.setTime(d);
|
||
yourd.add(Calendar.DAY_OF_YEAR, day);// 日期加
|
||
return yourd.getTime();
|
||
}
|
||
|
||
|
||
/**
|
||
* 判断某个时间是否过期。传进来一个旧时间,和有效天数,判断今天这个旧时间是否过期
|
||
*
|
||
* @param d
|
||
* 旧时间 "2019-8-5"
|
||
* @param day
|
||
* 有效天数
|
||
* @return true:没有过期;false:过期了
|
||
*/
|
||
public static boolean dateout(String d, int day) {
|
||
if (d == null || "".equals(d) || "null".equals(d))
|
||
return false;
|
||
Date date = Utils.dateadd(Utils.string2Date(d), day);
|
||
Date newdate = new Date();
|
||
int i = date.compareTo(newdate);
|
||
if (i >= 0) {
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 计算两个时间相差的秒数
|
||
*
|
||
* @param endDate
|
||
* 结束时间 null为当前
|
||
* @param nowDate
|
||
* 开始时间
|
||
* @return
|
||
*/
|
||
public static long datePoor(Date endDate, Date nowDate) {
|
||
|
||
if(endDate==null) endDate = new Date();
|
||
|
||
long ns = 1000;// 一秒
|
||
|
||
// 获得两个时间的毫秒时间差异
|
||
|
||
long diff = endDate.getTime() - nowDate.getTime();
|
||
|
||
long sec = diff / ns;// 计算差多少秒
|
||
return sec;
|
||
|
||
}
|
||
|
||
/**
|
||
* 计算两个时间相差的秒数
|
||
*
|
||
* @param endDate
|
||
* 结束时间 null为当前
|
||
* @param nowDate
|
||
* 开始时间
|
||
* @return
|
||
*/
|
||
public static long datePoor(String endDates, String nowDates) {
|
||
if(endDates==null) endDates=Utils.date2String(null, null);
|
||
Date endDate = Utils.string2Date(endDates, null);
|
||
Date nowDate = Utils.string2Date(nowDates, null);
|
||
|
||
long ns = 1000;// 一秒
|
||
|
||
// 获得两个时间的毫秒时间差异
|
||
|
||
long diff = endDate.getTime() - nowDate.getTime();
|
||
|
||
long sec = diff / ns;// 计算差多少秒
|
||
return sec;
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 设置定时器启动时间
|
||
* @param format 格式 yyyy-MM-dd 07:30:00
|
||
* @param d 星期,1-7,其他数字默认当天
|
||
* @return
|
||
*/
|
||
public static Date setTime(String format,int d) {
|
||
long daySpan=24 * 60 * 60 * 1000;
|
||
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
||
Date startTime = new Date();
|
||
try {
|
||
startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date()));
|
||
} catch (ParseException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
int dd = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);//今天是星期几
|
||
--dd;
|
||
if(dd==0) dd=7;
|
||
if(d>7) d=dd;
|
||
if(d<0){
|
||
if (System.currentTimeMillis() > startTime.getTime()){
|
||
startTime = new Date(startTime.getTime() + (daySpan));
|
||
}
|
||
}else if(d==dd){
|
||
if (System.currentTimeMillis() > startTime.getTime()){
|
||
startTime = new Date(startTime.getTime() + (daySpan*7));
|
||
}
|
||
}else{
|
||
if(d>dd){
|
||
startTime = new Date(startTime.getTime() + (daySpan*(d-dd-1)));
|
||
}else{
|
||
startTime = new Date(startTime.getTime() + (daySpan*(7-dd+d-1)));
|
||
}
|
||
}
|
||
|
||
return startTime;
|
||
}
|
||
|
||
|
||
/**
|
||
* 判断字符串是否为URL
|
||
*
|
||
* @param urls
|
||
* 需要判断的String类型url
|
||
* @return true:是URL;false:不是URL
|
||
*/
|
||
public static boolean isHttpUrl(String urls) {
|
||
boolean isurl = false;
|
||
String regex = "(((https|http)?://)?([a-z0-9]+[.])|(www.))"
|
||
+ "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)";// 设置正则表达式
|
||
|
||
Pattern pat = Pattern.compile(regex.trim());// 对比
|
||
Matcher mat = pat.matcher(urls.trim());
|
||
isurl = mat.matches();// 判断是否匹配
|
||
if (isurl) {
|
||
isurl = true;
|
||
}
|
||
return isurl;
|
||
}
|
||
|
||
/**
|
||
* 缩短网址
|
||
*
|
||
* @param longUrl
|
||
* @return shortUrl
|
||
*/
|
||
public static String shortUrl(String longUrl) {
|
||
// 以自己服务器做映射的修改
|
||
String s2 = "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long="
|
||
+ longUrl;
|
||
s2 = "http://sa.sogou.com/gettiny?url=" + longUrl;
|
||
return s2;
|
||
}
|
||
|
||
/**
|
||
* URL解析与转义
|
||
*
|
||
* @param url
|
||
* url
|
||
* @param flg
|
||
* 1:转义,-1:解析
|
||
* @return
|
||
*/
|
||
public static String doURL(String url, int flg) {
|
||
if (url == null || "".equals(url) || "null".equals(url))
|
||
return "";
|
||
String str = "";
|
||
if (flg == 1) {
|
||
try {
|
||
str = java.net.URLEncoder.encode(url, "UTF-8");// 转义
|
||
} catch (UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
}
|
||
} else if (flg == -1) {
|
||
try {
|
||
str = java.net.URLDecoder.decode(url, "UTF-8"); // 解析
|
||
} catch (UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return str;
|
||
}
|
||
|
||
/**
|
||
* 获取cookie的value
|
||
*
|
||
* @param cookies
|
||
* 数组
|
||
* @param name
|
||
* @return value
|
||
*/
|
||
public static String getcookie(Cookie[] cookies, String name) {
|
||
String valueString = "";
|
||
if (cookies != null) {
|
||
for (int i = 0; i < cookies.length; i++) {
|
||
if (name.equals(cookies[i].getName())) {
|
||
valueString = cookies[i].getValue();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return valueString;
|
||
}
|
||
|
||
/**
|
||
* * selvet PrintWriter 输出一个html提示页,并且5秒后跳转到url
|
||
*
|
||
* @param out
|
||
* @param context
|
||
* 弹框内容
|
||
* @param url
|
||
*/
|
||
public static void outHtml(PrintWriter out, String context, String url) {
|
||
// ///////////////////////输出HTML///////////////////////////////
|
||
out.println("<!DOCTYPE html>");
|
||
out.println("<html lang='en'>");
|
||
out.println("<head>");
|
||
out.println(" <meta charset='UTF-8'>");
|
||
out.println(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
|
||
out.println(" <meta http-equiv='X-UA-Compatible' content='ie=edge'>");
|
||
out.println(" <title>提示信息</title>");
|
||
out.println(" <style>");
|
||
out.println(" * {");
|
||
out.println(" font-family: '微软雅黑';");
|
||
out.println(" margin: 0;");
|
||
out.println(" padding: 0;");
|
||
out.println(" }");
|
||
out.println(" body {");
|
||
out.println(" background-color: #E5E5E5;");
|
||
out.println(" }");
|
||
out.println(" .tip-panel {");
|
||
out.println(" text-align: center;");
|
||
out.println(" margin: 105px auto;");
|
||
out.println(" width: 300px;");
|
||
out.println(" height: 400px;");
|
||
out.println(" border-radius: 10px;");
|
||
out.println(" box-shadow: 0px 0px 46px 0px #84807B;");
|
||
out.println(" background-color: #fff;");
|
||
out.println(" }");
|
||
out.println(" .tip-panel .time {");
|
||
out.println(" margin: 0 auto;");
|
||
out.println(" width: 160px;");
|
||
out.println(" height: 160px;");
|
||
out.println(" background-color: #fff;");
|
||
out.println(" border: 10px solid #f4f2f3;");
|
||
out.println(" border-radius: 50%;");
|
||
out.println(" position: relative;");
|
||
out.println(" top: -12%;");
|
||
out.println(" }");
|
||
out.println(" .tip-panel .time p {");
|
||
out.println(" font-size: 45px;");
|
||
out.println(" position: absolute;");
|
||
out.println(" top: 50%;");
|
||
out.println(" left: 50%;");
|
||
out.println(" transform: translate(-50%, -50%);");
|
||
out.println(" }");
|
||
out.println(" .tip-panel>p {");
|
||
out.println(" color: red;");
|
||
out.println(" margin-top: 15px;");
|
||
out.println(" }");
|
||
out.println(" .button-register {");
|
||
out.println(" padding: 13px;");
|
||
out.println(" margin-top: 20px;");
|
||
out.println(" display: inline-block;");
|
||
out.println(" background-color: #4aaffc;");
|
||
out.println(" text-decoration: none;");
|
||
out.println(" color: #fff;");
|
||
out.println(" border-radius: 6px;");
|
||
out.println(" }");
|
||
out.println(" </style>");
|
||
out.println("</head>");
|
||
out.println("<body>");
|
||
out.println(" <div class='tip-panel'>");
|
||
out.println(" <div class='time'>");
|
||
out.println(" <p>");
|
||
out.println(" <font id='time'></font>");
|
||
out.println(" </p>");
|
||
out.println(" </div>");
|
||
out.println(" <h2 style='color:#999'>" + context + "</h2>");
|
||
out.println(" <p>5秒后自动跳转</p>");
|
||
out.println(" <a href='" + url
|
||
+ "' class='button-register'>立即前往</a>");
|
||
out.println(" </div>");
|
||
out.println(" <script>");
|
||
out.println(" var i = 5;");
|
||
out.println(" var xx = window.setInterval(tt, 1000);");
|
||
out.println(" function tt() {");
|
||
out.println(" i--;");
|
||
out.println(" document.getElementById('time').innerHTML = i + '秒';");
|
||
out.println(" if (i == 0) {");
|
||
out.println(" window.clearInterval(xx);");
|
||
out.println(" window.location.href = '" + url + "';");
|
||
out.println(" }");
|
||
out.println(" }");
|
||
out.println(" </script>");
|
||
out.println("</body>");
|
||
out.println("</html>");
|
||
out.flush();
|
||
out.close();
|
||
// //////////////////////////////////////////////////
|
||
}
|
||
|
||
/**
|
||
* * springmvc return 输出一个html提示页,并且5秒后跳转到url
|
||
*
|
||
* @param context
|
||
* 弹框内容
|
||
* @param url
|
||
*/
|
||
public static String putHtml(String context, String url) {
|
||
// ///////////////////////输出HTML///////////////////////////////
|
||
String s = "";
|
||
s += "<!DOCTYPE html>";
|
||
s += "<html lang='en'>";
|
||
s += "<head>";
|
||
s += " <meta charset='UTF-8'>";
|
||
s += " <meta name='viewport' content='width=device-width, initial-scale=1.0'>";
|
||
s += " <meta http-equiv='X-UA-Compatible' content='ie=edge'>";
|
||
s += " <title>提示信息</title>";
|
||
s += " <style>";
|
||
s += " * {";
|
||
s += " font-family: '微软雅黑';";
|
||
s += " margin: 0;";
|
||
s += " padding: 0;";
|
||
s += " }";
|
||
s += " body {";
|
||
s += " background-color: #E5E5E5;";
|
||
s += " }";
|
||
s += " .tip-panel {";
|
||
s += " text-align: center;";
|
||
s += " margin: 105px auto;";
|
||
s += " width: 300px;";
|
||
s += " height: 400px;";
|
||
s += " border-radius: 10px;";
|
||
s += " box-shadow: 0px 0px 46px 0px #84807B;";
|
||
s += " background-color: #fff;";
|
||
s += " }";
|
||
s += " .tip-panel .time {";
|
||
s += " margin: 0 auto;";
|
||
s += " width: 160px;";
|
||
s += " height: 160px;";
|
||
s += " background-color: #fff;";
|
||
s += " border: 10px solid #f4f2f3;";
|
||
s += " border-radius: 50%;";
|
||
s += " position: relative;";
|
||
s += " top: -12%;";
|
||
s += " }";
|
||
s += " .tip-panel .time p {";
|
||
s += " font-size: 45px;";
|
||
s += " position: absolute;";
|
||
s += " top: 50%;";
|
||
s += " left: 50%;";
|
||
s += " transform: translate(-50%, -50%);";
|
||
s += " }";
|
||
s += " .tip-panel>p {";
|
||
s += " color: red;";
|
||
s += " margin-top: 15px;";
|
||
s += " }";
|
||
s += " .button-register {";
|
||
s += " padding: 13px;";
|
||
s += " margin-top: 20px;";
|
||
s += " display: inline-block;";
|
||
s += " background-color: #4aaffc;";
|
||
s += " text-decoration: none;";
|
||
s += " color: #fff;";
|
||
s += " border-radius: 6px;";
|
||
s += " }";
|
||
s += " </style>";
|
||
s += "</head>";
|
||
s += "<body>";
|
||
s += " <div class='tip-panel'>";
|
||
s += " <div class='time'>";
|
||
s += " <p>";
|
||
s += " <font id='time'></font>";
|
||
s += " </p>";
|
||
s += " </div>";
|
||
s += " <h2 style='color:#999'>" + context + "</h2>";
|
||
s += " <p>5秒后自动跳转</p>";
|
||
s += " <a href='" + url + "' class='button-register'>立即前往</a>";
|
||
s += " </div>";
|
||
s += " <script>";
|
||
s += " var i = 5;";
|
||
s += " var xx = window.setInterval(tt, 1000);";
|
||
s += " function tt() {";
|
||
s += " i--;";
|
||
s += " document.getElementById('time').innerHTML = i + '秒';";
|
||
s += " if (i == 0) {";
|
||
s += " window.clearInterval(xx);";
|
||
s += " window.location.href = '" + url + "';";
|
||
s += " }";
|
||
s += " }";
|
||
s += " </script>";
|
||
s += "</body>";
|
||
return s += "</html>";
|
||
// //////////////////////////////////////////////////
|
||
}
|
||
|
||
/**
|
||
* 返回刷新
|
||
* @return
|
||
*/
|
||
public static String goback(){
|
||
String s = "<script>javascript:window.location.href = document.referrer</script>";
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 读取config.properties的值
|
||
*
|
||
* @return
|
||
*/
|
||
public static String getValue(String name) {
|
||
String path = Utils.class.getResource("/").getPath();
|
||
String value = null;
|
||
Properties prop = new Properties();
|
||
try {
|
||
// 读取属性文件a.properties
|
||
FileInputStream in = new FileInputStream(path
|
||
+ "/config.properties");
|
||
prop.load(in); // /加载属性列表
|
||
value = prop.getProperty(name);
|
||
in.close();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return value;
|
||
}
|
||
|
||
/**
|
||
* 利用DBUtils查询的listmap集给request设置setAttribute(map.key,map.value)
|
||
*
|
||
* @param request
|
||
* @param arr
|
||
* db查询的结果集(只能有一条记录 arr.get(0) )
|
||
* @return
|
||
*/
|
||
public static HttpServletRequest setAttr(HttpServletRequest request,
|
||
List<Map<String, Object>> arr) {
|
||
HttpServletRequest r = request;
|
||
Map<String, Object> m = arr.get(0);
|
||
for (Map.Entry<String, Object> entry : m.entrySet()) {
|
||
String v = String.valueOf(entry.getValue());
|
||
if (v == null || "null".equals(v))
|
||
v = "";
|
||
r.setAttribute(entry.getKey(), v);
|
||
}
|
||
|
||
return r;
|
||
}
|
||
|
||
/**
|
||
* 如果是null、"null"、""、"undefined"、空格,都会返回"",否则返回原字符串
|
||
*
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static String checkNull(String str) {
|
||
String str1 = str.trim();
|
||
if (str1 == null || "".equals(str1) || "null".equals(str1)
|
||
|| "undefined".equals(str1))
|
||
return "";
|
||
else
|
||
return str;
|
||
}
|
||
|
||
/**
|
||
* 获取字符串中的数字(分正负)<br>
|
||
* 严格模式(查取完数字和符号,第二个开始到最后必须是数字)
|
||
*
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static Long getNum2(String str) {
|
||
|
||
str = str.trim();
|
||
String str2 = "";
|
||
if (str != null && !"".equals(str)) {
|
||
for (int i = 0; i < str.length(); i++) {
|
||
if ((str.charAt(i) >= 48 && str.charAt(i) <= 57)
|
||
|| str.charAt(i) == 43 || str.charAt(i) == 45) {
|
||
str2 += str.charAt(i);
|
||
}
|
||
}
|
||
}
|
||
boolean isok = true;
|
||
for (int i = 1; i < str2.length(); i++) {
|
||
if (str2.charAt(i) == 43 || str2.charAt(i) == 45) {
|
||
isok = false;
|
||
}
|
||
}
|
||
if (!isok)
|
||
return getNum(str);
|
||
if ("".equals(str2))
|
||
str2 = "0";
|
||
return Long.valueOf(str2);
|
||
}
|
||
|
||
/**
|
||
* 获取字符串中的时间<br>
|
||
* yyy-MM-dd
|
||
*
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static Long getDateString(String str) {
|
||
|
||
str = str.trim();
|
||
String str2 = "";
|
||
if (str != null && !"".equals(str)) {
|
||
for (int i = 0; i < str.length(); i++) {
|
||
if ((str.charAt(i) >= 48 && str.charAt(i) <= 57)
|
||
|| str.charAt(i) == 43 || str.charAt(i) == 45) {
|
||
str2 += str.charAt(i);
|
||
}
|
||
}
|
||
}
|
||
boolean isok = true;
|
||
for (int i = 1; i < str2.length(); i++) {
|
||
if (str2.charAt(i) == 43 || str2.charAt(i) == 45) {
|
||
isok = false;
|
||
}
|
||
}
|
||
if (!isok)
|
||
return getNum(str);
|
||
if ("".equals(str2))
|
||
str2 = "0";
|
||
return Long.valueOf(str2);
|
||
}
|
||
|
||
/**
|
||
* 获取字符串中的数字(分正负)<br>
|
||
* 严格模式(查取完数字和符号,第二个开始到最后必须是数字)
|
||
*
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static double getNumDouble(String str) {
|
||
|
||
str = str.trim();
|
||
String str2 = "";
|
||
if (str != null && !"".equals(str)) {
|
||
for (int i = 0; i < str.length(); i++) {
|
||
if ((str.charAt(i) >= 48 && str.charAt(i) <= 57)
|
||
|| str.charAt(i) == 43 || str.charAt(i) == 45
|
||
|| str.charAt(i) == 46) {
|
||
str2 += str.charAt(i);
|
||
}
|
||
}
|
||
}
|
||
boolean isok = true;
|
||
for (int i = 1; i < str2.length(); i++) {
|
||
if (str2.charAt(i) == 43 || str2.charAt(i) == 45) {
|
||
isok = false;
|
||
}
|
||
}
|
||
if (!isok)
|
||
return Double.valueOf(getNum(str));
|
||
if ("".equals(str2))
|
||
str2 = "0";
|
||
return Double.valueOf(str2);
|
||
}
|
||
|
||
/**
|
||
* 获取字符串中的数字
|
||
*
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static Long getNum(String str) {
|
||
|
||
str = str.trim();
|
||
String str2 = "";
|
||
if (str != null && !"".equals(str)) {
|
||
for (int i = 0; i < str.length(); i++) {
|
||
if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
|
||
str2 += str.charAt(i);
|
||
}
|
||
}
|
||
}
|
||
if ("".equals(str2))
|
||
str2 = "-1";
|
||
return Long.valueOf(str2);
|
||
}
|
||
|
||
/**获取今天是周几,1-7*/
|
||
public static long getWeekNum() {
|
||
long[] weekDays = {7,1,2,3,4,5,6};
|
||
Calendar c = Calendar.getInstance();
|
||
return weekDays[c.get(Calendar.DAY_OF_WEEK)-1];
|
||
}
|
||
|
||
/**
|
||
* 获取用户真实ip
|
||
*
|
||
* @param request
|
||
* @return
|
||
*/
|
||
public static String getIpAddr(HttpServletRequest request) {
|
||
String ip = request.getHeader("x-forwarded-for");
|
||
if ((ip == null) || (ip.length() == 0)
|
||
|| ("unknown".equalsIgnoreCase(ip))) {
|
||
ip = request.getHeader("Proxy-Client-IP");
|
||
}
|
||
if ((ip == null) || (ip.length() == 0)
|
||
|| ("unknown".equalsIgnoreCase(ip))) {
|
||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||
}
|
||
if ((ip == null) || (ip.length() == 0)
|
||
|| ("unknown".equalsIgnoreCase(ip))) {
|
||
ip = request.getRemoteAddr();
|
||
}
|
||
return ip;
|
||
}
|
||
|
||
/**
|
||
* 从字符串中截取出正确的时间
|
||
*
|
||
* @param stringTime
|
||
* @return
|
||
*/
|
||
public static Date cutDate(String stringTime) {
|
||
String regs[] = { "\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}时\\d{2}分",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}时",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}时",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}时",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}时",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}时", "\\d{4}年\\d{2}月\\d{2}日",
|
||
"\\d{4}年\\d{2}月\\d{1}日", "\\d{4}年\\d{1}月\\d{2}日",
|
||
"\\d{4}年\\d{1}月\\d{1}日",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}:\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{2}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\s\\d{1}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{2}",
|
||
"\\d{4}年\\d{1}月\\d{2}日\\s\\d{1}",
|
||
"\\d{4}年\\d{2}月\\d{2}日\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}", "\\d{4}-\\d{2}-\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{1}", "\\d{4}-\\d{1}-\\d{2}",
|
||
"\\d{4}-\\d{1}-\\d{1}",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}-\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{2}时",
|
||
"\\d{4}-\\d{2}-\\d{2}\\s\\d{1}时",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{2}时",
|
||
"\\d{4}-\\d{1}-\\d{2}\\s\\d{1}时", "\\d{4}.\\d{2}.\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{1}", "\\d{4}.\\d{1}.\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{1}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{2}",
|
||
"\\d{4}.\\d{2}.\\d{2}\\s\\d{1}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{2}",
|
||
"\\d{4}.\\d{1}.\\d{2}\\s\\d{1}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}时",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}时",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}时",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}时", "\\d{4}/\\d{2}/\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{1}", "\\d{4}/\\d{1}/\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{1}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{2}",
|
||
"\\d{4}/\\d{2}/\\d{2}\\s\\d{1}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{2}",
|
||
"\\d{4}/\\d{1}/\\d{2}\\s\\d{1}",
|
||
"\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}月\\d{2}日\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{1}月\\d{2}日\\s\\d{1}时\\d{2}分",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}时\\d{2}分",
|
||
"\\d{2}月\\d{2}日\\d{2}时\\d{2}分", "\\d{2}月\\d{2}日\\s\\d{2}时",
|
||
"\\d{2}月\\d{2}日\\s\\d{1}时", "\\d{1}月\\d{2}日\\s\\d{2}时",
|
||
"\\d{1}月\\d{2}日\\s\\d{1}时", "\\d{2}月\\d{2}日\\d{2}时",
|
||
"\\d{4}年\\d{2}月\\d{2}日", "\\d{2}月\\d{1}日",
|
||
"\\d{4}年\\d{1}月\\d{2}日", "\\d{1}月\\d{1}日",
|
||
"\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}:\\d{1}:\\d{2}",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}月\\d{2}日\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}月\\d{2}日\\s\\d{2}:\\d{2}",
|
||
"\\d{2}月\\d{2}日\\s\\d{1}:\\d{2}",
|
||
"\\d{1}月\\d{2}日\\s\\d{2}:\\d{2}",
|
||
"\\d{1}月\\d{2}日\\s\\d{1}:\\d{2}",
|
||
"\\d{2}月\\d{2}日\\d{2}:\\d{2}", "\\d{2}月\\d{2}日\\s\\d{2}",
|
||
"\\d{2}月\\d{2}日\\s\\d{1}", "\\d{1}月\\d{2}日\\s\\d{2}",
|
||
"\\d{1}月\\d{2}日\\s\\d{1}", "\\d{2}月\\d{2}日\\d{2}",
|
||
"\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{1}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{1}-\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{2}-\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{2}-\\d{2}\\s\\d{1}:\\d{2}", "\\d{2}-\\d{2}\\s\\d{2}",
|
||
"\\d{4}-\\d{2}-\\d{2}", "\\d{2}-\\d{1}",
|
||
"\\d{4}-\\d{1}-\\d{2}", "\\d{1}-\\d{1}",
|
||
"\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}-\\d{1}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}-\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{2}-\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{1}-\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{1}-\\d{2}\\s\\d{1}时\\d{2}分", "\\d{2}-\\d{2}\\s\\d{2}时",
|
||
"\\d{2}-\\d{2}\\s\\d{1}时", "\\d{1}-\\d{2}\\s\\d{2}时",
|
||
"\\d{1}-\\d{2}\\s\\d{1}时", "\\d{4}.\\d{2}.\\d{2}",
|
||
"\\d{2}.\\d{1}", "\\d{4}.\\d{1}.\\d{2}", "\\d{1}.\\d{1}",
|
||
"\\d{2}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{1}.\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{1}.\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{1}.\\d{1}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{2}.\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{2}.\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{1}.\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{1}.\\d{2}\\s\\d{1}:\\d{2}", "\\d{2}.\\d{2}\\s\\d{2}",
|
||
"\\d{2}.\\d{2}\\s\\d{1}", "\\d{1}.\\d{2}\\s\\d{2}",
|
||
"\\d{1}.\\d{2}\\s\\d{1}",
|
||
"\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分\\d{2}秒",
|
||
"\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分\\d{2}秒",
|
||
"\\d{2}/\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{2}/\\d{2}\\s\\d{1}时\\d{2}分",
|
||
"\\d{1}/\\d{2}\\s\\d{2}时\\d{2}分",
|
||
"\\d{1}/\\d{2}\\s\\d{1}时\\d{2}分", "\\d{2}/\\d{2}\\s\\d{2}时",
|
||
"\\d{2}/\\d{2}\\s\\d{1}时", "\\d{1}/\\d{2}\\s\\d{2}时",
|
||
"\\d{1}/\\d{2}\\s\\d{1}时", "\\d{2}/\\d{2}", "\\d{2}/\\d{1}",
|
||
"\\d{1}/\\d{2}", "\\d{1}/\\d{1}",
|
||
"\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{2}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{1}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}",
|
||
"\\d{1}/\\d{2}\\s\\d{1}:\\d{2}:\\d{2}",
|
||
"\\d{2}/\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{2}/\\d{2}\\s\\d{1}:\\d{2}",
|
||
"\\d{1}/\\d{2}\\s\\d{2}:\\d{2}",
|
||
"\\d{1}/\\d{2}\\s\\d{1}:\\d{2}", "\\d{2}/\\d{2}\\s\\d{2}",
|
||
"\\d{2}/\\d{2}\\s\\d{1}", "\\d{1}/\\d{2}\\s\\d{2}",
|
||
"\\d{1}/\\d{2}\\s\\d{1}", };
|
||
|
||
String str = "";
|
||
Date date = null;
|
||
for (String reg : regs) {
|
||
String temp = match(reg, stringTime);
|
||
if (temp.length() > str.length()) {
|
||
str = temp;
|
||
if (!"".equals(str)) {
|
||
date = formatDate(str);
|
||
}
|
||
}
|
||
|
||
}
|
||
return date;
|
||
|
||
}
|
||
|
||
/**
|
||
* 把String格式的时间转化为date<br>
|
||
* 支持多种时间格式
|
||
*
|
||
* @param stringTime
|
||
* @return
|
||
*/
|
||
public static Date formatDate(String stringTime) {
|
||
Date date = null;
|
||
if (StringUtils.isNotBlank(stringTime)) {
|
||
String[] pattern = new String[] { "yyyy年MM月dd日HH时mm分ss秒",
|
||
"yyyy年MM月dd日 HH时mm分ss秒", "yyyy年MM月dd日HH时mm分",
|
||
"yyyy年MM月dd日 HH时mm分", "yyyy年MM月dd日 HH时", "yyyy年MM月dd日HH时",
|
||
"yyyy年MM月dd日", "yyyy年MM月dd日HH:mm:ss",
|
||
"yyyy年MM月dd日 HH:mm:ss", "yyyy年MM月dd日HH:mm",
|
||
"yyyy年MM月dd日 HH:mm", "yyyy年MM月dd日 HH", "yyyy年MM月dd日HH",
|
||
"yyyy-MM-dd HH时mm分ss秒", "yyyy-MM-dd HH时mm分",
|
||
"yyyy-MM-dd HH时", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss",
|
||
"yyyy-MM-dd HH:mm", "yyyy-MM-dd HH",
|
||
"yyyy/MM/dd HH时mm分ss秒", "yyyy/MM/dd HH时mm分",
|
||
"yyyy/MM/dd HH时", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
|
||
"yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy.MM.dd HH:mm:ss",
|
||
"yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM.dd",
|
||
"yyyyMMdd", };
|
||
try {
|
||
date = DateUtils.parseDate(stringTime, pattern);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
return date;
|
||
|
||
}
|
||
|
||
/**
|
||
* 提取符合正则表达式的内容
|
||
*
|
||
* @param reg
|
||
* @param stringTime
|
||
* @return
|
||
*/
|
||
public static String match(String reg, String stringTime) {
|
||
Pattern p = Pattern.compile(reg);
|
||
Matcher m = p.matcher(stringTime);
|
||
String s = "";
|
||
if (m.find()) {
|
||
s += m.group();
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 获取字符编码
|
||
* @param str
|
||
* @return
|
||
*/
|
||
public static String getEncoding(String str) {
|
||
String encode;
|
||
|
||
encode = "UTF-16";
|
||
try {
|
||
if (str.equals(new String(str.getBytes(), encode))) {
|
||
return encode;
|
||
}
|
||
} catch (Exception ex) {
|
||
}
|
||
|
||
encode = "ASCII";
|
||
try {
|
||
if (str.equals(new String(str.getBytes(), encode))) {
|
||
return "字符串<< " + str + " >>中仅由数字和英文字母组成,无法识别其编码格式";
|
||
}
|
||
} catch (Exception ex) {
|
||
}
|
||
|
||
encode = "ISO-8859-1";
|
||
try {
|
||
if (str.equals(new String(str.getBytes(), encode))) {
|
||
return encode;
|
||
}
|
||
} catch (Exception ex) {
|
||
}
|
||
|
||
encode = "GB2312";
|
||
try {
|
||
if (str.equals(new String(str.getBytes(), encode))) {
|
||
return encode;
|
||
}
|
||
} catch (Exception ex) {
|
||
}
|
||
|
||
encode = "UTF-8";
|
||
try {
|
||
if (str.equals(new String(str.getBytes(), encode))) {
|
||
return encode;
|
||
}
|
||
} catch (Exception ex) {
|
||
}
|
||
|
||
/*
|
||
* ......待完善
|
||
*/
|
||
|
||
return "gbk";
|
||
}
|
||
|
||
/**
|
||
* 把其他格式编码字符串转为utf8
|
||
* @return
|
||
*/
|
||
public static String toUTF8(String str) {
|
||
String rs="";
|
||
try {
|
||
String en = getEncoding(str);
|
||
rs=new String(str.getBytes(en),"utf-8");
|
||
} catch (UnsupportedEncodingException e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return rs;
|
||
}
|
||
|
||
//正则找出所有
|
||
public static String getUrl(String s){
|
||
if(s==null||s.length()<1) return null;
|
||
String regex = "http[s]?:\\/\\/([\\w]+\\.)+[\\w]+([\\w./?%&=]*)?";
|
||
Pattern pattern = Pattern.compile(regex);
|
||
Matcher m = pattern.matcher(s);
|
||
String r="";
|
||
while(m.find()){
|
||
r+=m.group()+",";
|
||
}
|
||
return r;
|
||
}
|
||
/**
|
||
* 将小数保留n位
|
||
* @param d 小数
|
||
* @param n 保留位数
|
||
* @param x 小数为0时是否保留
|
||
* @return
|
||
*/
|
||
public static String floatToString(double d,int n,boolean x) {
|
||
String f=".";
|
||
for(int i=0;i<n;i++){
|
||
f+="0";
|
||
}
|
||
DecimalFormat decimalFormat=new DecimalFormat(f);//构造方法的字符格式这里如果小数不足2位,会以0补足.
|
||
String p=decimalFormat.format(d);//format 返回的是字符串
|
||
if(p.split("\\.")[0].length()<1) p="0"+p;
|
||
String nn ="0";
|
||
if(p.split("\\.").length>1) nn = p.split("\\.")[1];
|
||
if(!x){
|
||
if(nn.replaceAll("0", "").length()<1) p=p.split("\\.")[0];
|
||
}
|
||
|
||
return p;
|
||
}
|
||
|
||
/**
|
||
* 将小数保留n位
|
||
* @param d 小数
|
||
* @param n 保留位数
|
||
* @param x 小数为0时是否保留
|
||
* @return
|
||
*/
|
||
public static String floatToString(float d,int n,boolean x) {
|
||
String f=".";
|
||
for(int i=0;i<n;i++){
|
||
f+="0";
|
||
}
|
||
DecimalFormat decimalFormat=new DecimalFormat(f);//构造方法的字符格式这里如果小数不足2位,会以0补足.
|
||
String p=decimalFormat.format(d);//format 返回的是字符串
|
||
if(p.split("\\.")[0].length()<1) p="0"+p;
|
||
String nn = p.split("\\.")[1];
|
||
if(!x){
|
||
if(nn.replaceAll("0", "").length()<1) p=p.split("\\.")[0];
|
||
}
|
||
|
||
return p;
|
||
}
|
||
|
||
/**
|
||
* 生成随机字符串(字母数字混合)
|
||
* @param length 几位
|
||
* @return
|
||
*/
|
||
public static String random(int length){
|
||
|
||
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||
|
||
Random random=new Random();
|
||
|
||
StringBuffer sb=new StringBuffer();
|
||
|
||
for(int i=0;i<length;i++){
|
||
|
||
int number=random.nextInt(62); //从62个字符中随机取其中一个
|
||
|
||
sb.append(str.charAt(number)); //用取到的数当索引取字符加到length个数的字符串
|
||
|
||
}
|
||
|
||
return sb.toString(); //返回字符串
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取html字符中的图片地址
|
||
* @param htmlStr
|
||
* @param quchong 是否相邻图片地址去重
|
||
* @return
|
||
*/
|
||
public static ArrayList<String> getImgStr(String htmlStr,boolean quchong) {
|
||
ArrayList<String> pics = new ArrayList<>();
|
||
String img = "";
|
||
Pattern p_image;
|
||
Matcher m_image;
|
||
String last_img="";
|
||
// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
|
||
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
|
||
p_image = Pattern.compile
|
||
(regEx_img, Pattern.CASE_INSENSITIVE);
|
||
m_image = p_image.matcher(htmlStr);
|
||
while (m_image.find()) {
|
||
// 得到<img />数据 不懂的qq1023732997
|
||
img = m_image.group();
|
||
// 匹配<img>中的src数据
|
||
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
|
||
while (m.find()) {
|
||
if(last_img.equals(m.group(1)) && quchong==true) continue;
|
||
pics.add(m.group(1));
|
||
last_img=m.group(1);
|
||
}
|
||
}
|
||
return pics;
|
||
}
|
||
|
||
/**
|
||
* 获取图片的宽高
|
||
* @param url
|
||
* @return
|
||
*/
|
||
public static int[] getImgWH(String url){
|
||
int srcWidth = 0; // 源图宽度
|
||
int srcHeight = 0; // 源图高度
|
||
|
||
try{
|
||
|
||
URL url1 = new URL(url);
|
||
URLConnection connection = url1.openConnection();
|
||
connection.setDoOutput(true);
|
||
BufferedImage image = ImageIO.read(connection.getInputStream());
|
||
if(image==null) return new int[]{0,0};
|
||
srcWidth = image .getWidth(); // 源图宽度
|
||
srcHeight = image .getHeight(); // 源图高度
|
||
|
||
}catch(Exception e){e.printStackTrace();}
|
||
|
||
return new int[]{srcWidth,srcHeight};
|
||
}
|
||
|
||
|
||
public static Map<String, Object> urlParamToMaps(String q) {
|
||
|
||
if(q==null||"".equals(q.trim())) return null;
|
||
String[] a1 = q.split("&");
|
||
if(a1.length>0){
|
||
Map<String, Object> m = new HashMap<>();
|
||
for (String s : a1) {
|
||
String[] a2 = s.split("=");
|
||
if(a2.length>1){
|
||
m.put(a2[0], a2[1]);
|
||
}
|
||
}
|
||
return m;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
|
||
}
|