package com.ways.app.dpca.utils; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 公用类 * @author linyoucheng * @date 2014-1-14 * @company ways */ public class Convert { public static Date StringToDate(String str){ Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if(str!=null&&(!str.trim().equals(""))){ try { date = sdf.parse(str); } catch (Exception e) { e.printStackTrace(); } } return date; } public static String dateToString(Date date){ String s = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if(date!=null){ try { s = sdf.format(date); } catch (Exception e) { e.printStackTrace(); } } return s; } /** * 判断时间大于或者等于当天 * * @param day * 时间 * @return */ public static boolean isGreaterOrEquToday(Date day) { java.util.Calendar tc = Calendar.getInstance(); tc.setTime(day); Calendar td = Calendar.getInstance(); td.set(Calendar.HOUR_OF_DAY, 0); td.set(Calendar.MINUTE, 0); td.set(Calendar.SECOND, 0); td.set(Calendar.MILLISECOND, 0); return tc.after(td) || tc.equals(td); } /** * 判断时间小于或者等于当天 * * @param day * 时间 * @return */ public static boolean isLessOrEquToday(Date day) { java.util.Calendar tc = Calendar.getInstance(); tc.setTime(day); Calendar td = Calendar.getInstance(); td.set(Calendar.HOUR_OF_DAY, 0); td.set(Calendar.MINUTE, 0); td.set(Calendar.SECOND, 0); td.set(Calendar.MILLISECOND, 0); return tc.before(td) || tc.equals(td); } /** * @Title: isToday * @Description: 判断day日期是否是今天 * @param day * @return * @throws */ public static boolean isToday(Date day) { java.util.Calendar tc = Calendar.getInstance(); tc.setTime(day); Calendar td = Calendar.getInstance(); return tc.get(Calendar.YEAR) == td.get(Calendar.YEAR) && tc.get(Calendar.MONTH) == td.get(Calendar.MONTH) && tc.get(Calendar.DAY_OF_MONTH) == td .get(Calendar.DAY_OF_MONTH); } /** * @Title: isToday * @Description: 判断是否是当月 * @param day * @return * @throws */ public static boolean isCurrentMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); Calendar now = Calendar.getInstance(); boolean bl = c.get(Calendar.YEAR) == now.get(Calendar.YEAR)&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH); return bl; } /** * 获取前N年到当前年 * @param num 多少年 * @return */ public static String getYearStr(int num,int endYear) { //Calendar now = Calendar.getInstance(); String yearStr = ""; for (int i = 0; i < num; i++) { yearStr+=(endYear-i)+","; } if(yearStr.length()>1){ yearStr = yearStr.substring(0,yearStr.length()-1); } return yearStr; } /** * 两整数相除 * @param x * @param y * @return */ public static Double division(Integer x,Integer y){ if(x==null||y==null){ return -1d; } Double d = -1d; try { if(y!=0){ d = (double)x/(double)y; } if(d.isInfinite()){//如果是无穷大或无穷小,则设为NULL d = -1d; } } catch (Exception e) { } return d; } /** * 截取一个小数的方法 * @param d double类型的数据 * @return 保留小数后一位的字符串 */ public static String convertShortString(Double d){ String s = null; if(d!=null){ if(d==-1){ s = "-"; }else{ double db = (double)Math.round(d*1000)/10; s = db+"%"; } } return s; } /** * 截取N小数的方法 * @param d double类型的数据 * @return 保留N位小数后一位的字符串 */ public static String convertShortString(Double d,int n){ if(n<=0){ n=0; } String s = null; if(d!=null){ if(n==0){ int db = (int)(Math.round(d*Math.pow(10, n+2))/Math.pow(10, n)); s = db+"%"; }else{ double db = (double)Math.round(d*Math.pow(10, n+2))/Math.pow(10, n); s = db+"%"; } }else{ s = "-"; } return s; } /** * 截取N小数的方法,正数在前面加加号 * @param d double类型的数据 * @return 保留N位小数后一位的字符串 */ public static String convertShortStringChange(Double d,int n){ if(n<=0){ n=0; } String s = null; if(d!=null){ if(n==0){ int db = (int)(Math.round(d*Math.pow(10, n+2))/Math.pow(10, n)); s = db+"%"; }else{ double db = (double)Math.round(d*Math.pow(10, n+2))/Math.pow(10, n); s = db+"%"; } if(d>0){ s = "+"+s; } }else{ s = "-"; } return s; } /** * 浮点型匹配N位小数 * @param d 浮点型 * @param n 精度 * @return N位精度的字符串 */ public static String formatNPrecision(Double d,int n){ if(d==null){ return null; } String fm = "%0$02."+n+"f"; String s = String.format(fm, d); return s; } /** * 浮点型*100匹配N位小数 * @param d 浮点型 * @param n 精度 * @return N位精度的字符串 */ public static String formatNPrecisionPercent(Double d,int n){ if(d==null){ return null; } d = d*100; String fm = "%0$02."+n+"f"; String s = String.format(fm, d); return s; } /** * 过滤空值 * @param in * @return */ public static String filterEmpty(Integer in){ return in==null?"-":in.toString(); } public static String filterEmpty(Double in){ return in==null||in<=0?"-":in.toString();//百分比<0的也用“-”表示 } public static String filterEmpty(String in){ return in==null?"-":in.toString(); } public static String filterEmpty(Long in){ return in==null?"-":in.toString(); } /** * 数字匹配 * @param num 需匹配的对象 * @param pattern 匹配类型 * @return */ public static String numberFormat(int num,String pattern){ String s = null; try { java.text.DecimalFormat df = new java.text.DecimalFormat(pattern); s = df.format(num); } catch (Exception e) { e.printStackTrace(); } return s; } /** * 除以10000 * @param num * @return */ public static String divideTenThousand(int num,boolean bl){ String numStr = num+""; if(bl){ numStr = formatNPrecision((double)num/10000,1); } return numStr; } /** * 除以10000 * @param num * @return */ public static String divideTenThousand(long num,boolean bl){ String numStr = num+""; if(bl){ numStr = formatNPrecision((double)num/10000,1); } return numStr; } public static String convertShortPercent(Double d,int num) { String s = convertShortString(d,num); if(s!=null&&s.contains("%")){ s = s.replace("%", ""); } return s; } /** * 冒泡法升序排列 * @param sArr * @return */ public static int[] sortByUp(String sArr[]){ if(sArr==null){ return null; } int iArr[] = new int[sArr.length]; for (int i = 0; i < sArr.length; i++) { iArr[i] = Integer.parseInt(sArr[i]); } if(sArr.length<=1){ return iArr; } for (int i = 0; i < iArr.length-1; i++) { for (int j = 1; j < iArr.length-i; j++) { if(iArr[j]<iArr[j-1]){ int temp = iArr[j]; iArr[j] = iArr[j-1]; iArr[j-1] = temp; } } } return iArr; } /** * 冒泡法升序排列 * @param iArr * @return */ public static int[] sortByUp(int iArr[]){ if(iArr==null||iArr.length<=1){ return iArr; } for (int i = 0; i < iArr.length-1; i++) { for (int j = 1; j < iArr.length-i; j++) { if(iArr[j]<iArr[j-1]){ int temp = iArr[j]; iArr[j] = iArr[j-1]; iArr[j-1] = temp; } } } return iArr; } /** * 提供精确的加法运算 * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public static double add(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); }; /** * 提供精确的减法运算 * @param v1 被减数 * @param v2 减数 * @return 两个参数的差 */ public static double sub(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.subtract(b2).doubleValue(); } /** * 提供精确的乘法运算。 * @param v1 被乘数 * @param v2 乘数 * @return 两个参数的积 */ public static double mul(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); } /** * 计算某个月份的最大日期 * @param year * @param month * @return */ public static int maxDay(String year,String month){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Calendar cal = Calendar.getInstance(); Date dd = null; try { dd = sdf.parse(year+month); } catch (ParseException e) { e.printStackTrace(); } cal.setTime(dd); int day=cal.getActualMaximum(Calendar.DAY_OF_MONTH); return day; } //字符串转整型 public static Integer stringToInt(String str){ Integer result = null; try { if(str!=null&&(!str.trim().equals(""))){ result = Integer.parseInt(str); } } catch (Exception e) { e.printStackTrace(); } return result; } }