From c843272f0390ee22cc1ec1ec7bfad1763c43d17b Mon Sep 17 00:00:00 2001 From: admin <1782158860@qq.com> Date: Wed, 9 Nov 2022 16:11:16 +0800 Subject: [PATCH] add --- src/sc545/pay/utils/ReadTxt.java | 710 +++++++++++++++++ src/sc545/pay/utils/Utils.java | 1253 ++++++++++++++++++++++++++++++ 2 files changed, 1963 insertions(+) create mode 100644 src/sc545/pay/utils/ReadTxt.java create mode 100644 src/sc545/pay/utils/Utils.java diff --git a/src/sc545/pay/utils/ReadTxt.java b/src/sc545/pay/utils/ReadTxt.java new file mode 100644 index 0000000..ad3c9d1 --- /dev/null +++ b/src/sc545/pay/utils/ReadTxt.java @@ -0,0 +1,710 @@ +package sc545.pay.utils; + +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map.Entry; + +import javax.servlet.ServletContext; + +public class ReadTxt { + + public static void main(String[] args) { + } + + /** + * 按行读取txt文件 + * + * @param path + * 文件地址(默认D:\\scbox_settings开头) + * @return ArrayList 返回每行数据为一个items的集合 + */ + public static ArrayList readFileLine(ServletContext servletContext, String path) { + + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + + + ArrayList strarr = null; + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + strarr = new ArrayList(); + while ((lineTxt = bufferedReader.readLine()) != null) { + + strarr.add(lineTxt); + } + + read.close(); + } else { + strarr = null; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return strarr; + } + + + + /** + * 写入txt文档,已存在就修改,否则写入末尾,自动加换行 (文件格式:key=value,换行分隔) + * + * @param path + * 文件地址(默认D:\\scbox_settings开头) + * @param keyname + * 要修改的参数名 + * @param content + * 要修改的参数值 + */ + public static void writeSetting(ServletContext servletContext, String path, String keyname, String content) { + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + boolean isread = false;// 是否读取到该行并修改 + // 将内容全部读出来,存为新的数据流 + // 读到要修改的行进行修改存入,否则原样读取存入 + StringBuffer buf = new StringBuffer(); + while ((lineTxt = bufferedReader.readLine()) != null) { + if (lineTxt.startsWith(keyname + "=")) { + buf.append(keyname + "=" + content + "\r\n"); + isread = true; + } else { + buf.append(lineTxt + "\r\n"); + } + } + read.close(); + if (!isread) {// 没有读取到,写入末尾 + buf.append(keyname + "=" + content + "\r\n"); + } + + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(buf.toString()); + out.flush(); + out.close(); + + } else {// 不存在,创建写入 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(keyname + "=" + content + "\r\n"); + out.flush(); + out.close(); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + /** + * 读取txt配置,返回map集合(文件格式:key=value,换行分隔) + * + * @param path + * 文件地址(默认D:\\scbox_settings开头) + * @return hashmap + */ + public static HashMap readSetting(ServletContext servletContext, String path) { + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + HashMap strarr = null; + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + strarr = new HashMap(); + while ((lineTxt = bufferedReader.readLine()) != null) { + if(lineTxt.indexOf("#")==0){ + continue; + } + String[] arr = new String[2]; + if (lineTxt.split("=").length >= 2) { + arr[0] = lineTxt.split("=")[0]; + arr[1] = lineTxt.replace(arr[0]+"=", ""); + strarr.put(arr[0], arr[1]); + } + } + + read.close(); + } else { + strarr = null; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return strarr; + } + + + /** + * 获取配置文件中所有设置属性(含注释)
+ * map.get("z") 取注释
+ * map.get("k") 取key
+ * map.get("v") 取value
+ * + * + * @param path + * 文件地址(默认D:\\scbox_settings开头) + * + * @param t 只取以此字符开头的,不限制填null + * @param st 从以此字符开头的行开始读取,不限制填null + * @param et 读到以此字符开头的行结束,不限制填null + * @return + */ + public static ArrayList> getSettingAll(ServletContext servletContext, String path,String t,String st,String et) { + if(st==null||st.length()<1) st=""; + if(et==null||et.length()<1) et=""; + if(t==null||t.length()<1) t=""; + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + ArrayList> list=new ArrayList<>(); + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + String zs="";//注释文本 + while ((lineTxt = bufferedReader.readLine()) != null) { + if((!"".equals(st))||lineTxt.indexOf(st)!=0) continue; + if((!"".equals(et))&&lineTxt.indexOf(et)==0) break; + HashMap map = new HashMap(); + if(lineTxt.indexOf("#")==0){ + zs=lineTxt; + continue; + } + String[] arr = new String[2]; + if (lineTxt.split("=").length >= 2&&lineTxt.indexOf(t)==0) { + arr[0] = lineTxt.split("=")[0]; + arr[1] = lineTxt.replace(arr[0]+"=", ""); + map.put("z", zs); + map.put("k", arr[0]); + map.put("v", arr[1]); + list.add(map); + //把注释也加进去 + zs=""; + } + } + + read.close(); + } else { + list = null; + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return list; + } + + /** + * 读取txt配置文件的某一项设置值 (文件格式:key=value,换行分隔) + * + * @param path + * 文件地址 + * @param settingName + * 设置名 + * @param nullString + * 设置值为空时默认返回的内容 + * @return string + */ + public static String getSetting(ServletContext servletContext, String path, String settingName,String nullString) { + HashMap setting = ReadTxt.readSetting(servletContext,path); + String val = setting == null ? nullString : setting.get(settingName); + val = val == null ? "" : val; + return "".equals(val.trim()) ? nullString : val; + } + + /** + * 获取所有设置名以[settingName]开头的数据 + * */ + public static ArrayList getSettingW(ServletContext servletContext, String path, String settingName) { + HashMap setting = ReadTxt.readSetting(servletContext,path); + if(setting==null||setting.size()<1) return null; + Iterator> it = setting.entrySet().iterator(); + ArrayList rs=new ArrayList<>(); + while (it.hasNext()) { + Entry en=it.next(); + String k = en.getKey(); + String v = en.getValue(); + if(k.indexOf(settingName)==0){ + rs.add(k+"="+v); + } + } + + return rs; + } + + /** + * 删除某一项设置值 (文件格式:key=value,换行分隔) + * + * @param path + * 文件地址 + * @param settingName + * 设置名 + * @param nullString + * 设置值为空时默认返回的内容 + * @return string + */ + public static void delSetting(ServletContext servletContext, String path, String settingName) { + + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + // 将内容全部读出来,存为新的数据流 + // 读到要修改的行进行修改存入,否则原样读取存入 + StringBuffer buf = new StringBuffer(); + while ((lineTxt = bufferedReader.readLine()) != null) { + if (lineTxt.startsWith(settingName + "=")) { + } else { + buf.append(lineTxt + "\r\n"); + } + } + read.close(); + + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(buf.toString()); + out.flush(); + out.close(); + + } else {// 不存在,创建 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + + /** + * 删除设置名以[settingName]开头的数据 + * @param servletContext + * @param path + * @param settingName + */ + public static void delSettingW(ServletContext servletContext, String path, String settingName) { + + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + // 将内容全部读出来,存为新的数据流 + // 读到要修改的行进行修改存入,否则原样读取存入 + StringBuffer buf = new StringBuffer(); + while ((lineTxt = bufferedReader.readLine()) != null) { + if (lineTxt.split("=")[0].indexOf(settingName)==0) { + } else { + buf.append(lineTxt + "\r\n"); + } + } + read.close(); + + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(buf.toString()); + out.flush(); + out.close(); + + } else {// 不存在,创建 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + + /** + * 删除过期的所有设置值 (文件格式:key=value,换行分隔) + *
key为检测源 + * @param servletContext + * @param path 路径 + * @param d 有效天数 + */ + public static void delOutDateSetting(ServletContext servletContext, String path, int d) { + + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + // 将内容全部读出来,存为新的数据流 + // 读到要修改的行进行修改存入,否则原样读取存入 + StringBuffer buf = new StringBuffer(); + boolean ts=false; + while ((lineTxt = bufferedReader.readLine()) != null) { + + if(lineTxt.indexOf("=")>0&&lineTxt.indexOf("#")<0){ + String[] ls = lineTxt.split("="); + int has = ls[0].length()-ls[0].replaceAll("-", "").length(); + if(has>1){//日期时间 + if(Utils.dateout(ls[0], d)){ + buf.append(lineTxt + "\r\n"); + }else{ + ts=true; + }//过期 + }else{//月份时间 + buf.append(lineTxt + "\r\n"); + } + + }else{//注释或者不是setting + buf.append(lineTxt + "\r\n"); + } + + } + read.close(); + if(ts){ + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(buf.toString()); + out.flush(); + out.close(); + } + + } else {// 不存在,创建 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + + + /** + * 删除过期的所有设置值 (文件格式:key=value,换行分隔) + *
value为检测源 + * @param servletContext + * @param path 路径 + * @param d 有效天数 + */ + public static void delOutDateSettingValue(ServletContext servletContext, String path, int d) { + + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + InputStreamReader read = new InputStreamReader( + new FileInputStream(file), "UTF-8"); + BufferedReader bufferedReader = new BufferedReader(read); + String lineTxt = null; + // 将内容全部读出来,存为新的数据流 + // 读到要修改的行进行修改存入,否则原样读取存入 + StringBuffer buf = new StringBuffer(); + boolean ts=false; + while ((lineTxt = bufferedReader.readLine()) != null) { + + if(lineTxt.indexOf("=")>0&&lineTxt.indexOf("#")<0){ + String[] ls = lineTxt.split("="); + int has = ls[1].length()-ls[1].replaceAll("-", "").length(); + if(has>1){//日期时间 + if(Utils.dateout(ls[1], d)){ + buf.append(lineTxt + "\r\n"); + }else{ + ts=true; + }//过期 + }else{//月份时间 + buf.append(lineTxt + "\r\n"); + } + + }else{//注释或者不是setting + buf.append(lineTxt + "\r\n"); + } + + } + read.close(); + if(ts){ + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write(buf.toString()); + out.flush(); + out.close(); + } + + } else {// 不存在,创建 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + + /** + * 删除全部内容 + * + * @param path + * 文件地址 + * @param nullString + * 设置值为空时默认返回的内容 + * @return string + */ + public static void delAll(ServletContext servletContext, String path) { + + + /**************/ + // 如果存在,则追加内容;如果文件不存在,则创建文件 + if(path.indexOf("./")==0) + path =path.substring(1, path.length()); + + if (servletContext == null) { + if(path.indexOf(":") <= 0&&path.indexOf("/") <0) + path = "D:\\scbox_settings\\" + path; + } else { + path = servletContext.getRealPath(path); + } + + try { + File file = new File(path); + if (file.isFile() && file.exists()) { // 判断文件是否存在 + + // 把新内容覆盖写入文件 + PrintWriter out = new PrintWriter(new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(path), + "UTF-8"))); + out.write("".toString()); + out.flush(); + out.close(); + + } else {// 不存在,创建 + String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/")); + File file1 = new File(path2); + // 如果文件夹不存在则创建 + if (!file1.exists() && !file1.isDirectory()) { + file1.mkdirs(); + } + File file2 = new File(path); + if (!file2.exists()) { + file2.createNewFile(); + } + + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /**************/ + + } + + + + /** + * 将InputStream装换成某种字符编码的String + * + * @param in + * @param encoding + * @return + */ + public static String InputStreamTOString(InputStream in, String encoding) { + int BUFFER_SIZE = 4096; + String res = null; + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + byte[] data = new byte[BUFFER_SIZE]; + int count = -1; + try { + while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) { + outputStream.write(data, 0, count); + } + } catch (Exception e) { + e.printStackTrace(); + } + try { + res = new String(outputStream.toByteArray(), encoding); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return res; + } + + +} diff --git a/src/sc545/pay/utils/Utils.java b/src/sc545/pay/utils/Utils.java new file mode 100644 index 0000000..21675d1 --- /dev/null +++ b/src/sc545/pay/utils/Utils.java @@ -0,0 +1,1253 @@ +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; + } + + /** + * 字符串转时间
+ * 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(""); + out.println(""); + out.println(""); + out.println(" "); + out.println(" "); + out.println(" "); + out.println(" 提示信息"); + out.println(" "); + out.println(""); + out.println(""); + out.println("
"); + out.println("
"); + out.println("

"); + out.println(" "); + out.println("

"); + out.println("
"); + out.println("

" + context + "

"); + out.println("

5秒后自动跳转

"); + out.println(" 立即前往"); + out.println("
"); + out.println(" "); + out.println(""); + out.println(""); + 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 += ""; + s += ""; + s += ""; + s += " "; + s += " "; + s += " "; + s += " 提示信息"; + s += " "; + s += ""; + s += ""; + s += "
"; + s += "
"; + s += "

"; + s += " "; + s += "

"; + s += "
"; + s += "

" + context + "

"; + s += "

5秒后自动跳转

"; + s += " 立即前往"; + s += "
"; + s += " "; + s += ""; + return s += ""; + // ////////////////////////////////////////////////// + } + + /** + * 返回刷新 + * @return + */ + public static String goback(){ + String s = ""; + 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> arr) { + HttpServletRequest r = request; + Map m = arr.get(0); + for (Map.Entry 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; + } + + /** + * 获取字符串中的数字(分正负)
+ * 严格模式(查取完数字和符号,第二个开始到最后必须是数字) + * + * @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); + } + + /** + * 获取字符串中的时间
+ * 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); + } + + /** + * 获取字符串中的数字(分正负)
+ * 严格模式(查取完数字和符号,第二个开始到最后必须是数字) + * + * @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
+ * 支持多种时间格式 + * + * @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;i1) 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 getImgStr(String htmlStr,boolean quchong) { + ArrayList pics = new ArrayList<>(); + String img = ""; + Pattern p_image; + Matcher m_image; + String last_img=""; + // String regEx_img = "]*?>"; //图片链接地址 + String regEx_img = "]*?>"; + p_image = Pattern.compile + (regEx_img, Pattern.CASE_INSENSITIVE); + m_image = p_image.matcher(htmlStr); + while (m_image.find()) { + // 得到数据 不懂的qq1023732997 + img = m_image.group(); + // 匹配中的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 urlParamToMaps(String q) { + + if(q==null||"".equals(q.trim())) return null; + String[] a1 = q.split("&"); + if(a1.length>0){ + Map 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; + } + + + +}