package com.mzeat.util; import java.text.DecimalFormat; 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; public class StringUtils { public static boolean isEmpty(String str) { return null == str || str.length() == 0; } public static String formatDouble(double value) { DecimalFormat df = new DecimalFormat(); String style = "0.000000"; df.applyPattern(style);// 将格式应用于格式化器 return df.format(value); } public static String formatDistance(String distance) { int length = distance.length(); if (length <= 3) { distance = distance + "米"; } else { double dis = Double.valueOf(distance); dis = dis / 1000; java.text.DecimalFormat df = new java.text.DecimalFormat("#.00"); distance = String.valueOf(df.format(dis)) + "公里"; } return distance; } public static String formatAvg_point(String avg_point) { String start_num; double num = Double.valueOf(String.valueOf(avg_point)); if (num == 0.0f) { start_num = "0"; } else { java.text.DecimalFormat df = new java.text.DecimalFormat("#.0"); start_num = String.valueOf(df.format(num)); } return start_num; } /* * 验证号码 手机号 固话均可 */ public static boolean isPhoneNumberValid(String phoneNumber) { boolean isValid = false; String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))"; CharSequence inputStr = phoneNumber; Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(inputStr); if (matcher.matches()) { isValid = true; } return isValid; } /** * 手机号验证 * * @param str * @return 验证通过返回true */ public static boolean isMobile(String str) { Pattern p = null; Matcher m = null; boolean b = false; p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 验证手机号 m = p.matcher(str); b = m.matches(); return b; } /** * 电话号码验证 * * @param str * @return 验证通过返回true */ public static boolean isPhone(String str) { Pattern p1 = null, p2 = null; Matcher m = null; boolean b = false; p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的 p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的 if (str.length() > 9) { m = p1.matcher(str); b = m.matches(); } else { m = p2.matcher(str); b = m.matches(); } return b; } public static boolean checkDistance(String distance) { if (distance.length() > 6) { return false; } return true; } public static boolean isValidDate(String date){ if (date.length() <8) { return false; }else { if (date.length() > 10) { return false; }else { return true; } } } /** * 检测邮件的有效性 * * @param email * @return */ public static boolean validateEmail(String email) { boolean b = false; if (!("").equals(email)) { // b = email.matches("\\w+@\\w+\\.(com\\.cn)|\\w+@\\w+\\.(com|cn)"); b = email .matches("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); } return b; } private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); //private final static SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //private final static SimpleDateFormat dateFormater2 = new SimpleDateFormat("yyyy-MM-dd"); private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } }; /** * 将字符串转位日期类型 * @param sdate * @return */ public static Date toDate(String sdate) { try { return dateFormater.get().parse(sdate); } catch (ParseException e) { return null; } } /** * 以友好的方式显示时间 * @param sdate * @return */ public static String friendly_time(String sdate) { Date time = toDate(sdate); if(time == null) { return "Unknown"; } String ftime = ""; Calendar cal = Calendar.getInstance(); //判断是否是同一天 String curDate = dateFormater2.get().format(cal.getTime()); String paramDate = dateFormater2.get().format(time); if(curDate.equals(paramDate)){ int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000); if(hour == 0) ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000,1)+"分钟前"; else ftime = hour+"小时前"; return ftime; } long lt = time.getTime()/86400000; long ct = cal.getTimeInMillis()/86400000; int days = (int)(ct - lt); if(days == 0){ int hour = (int)((cal.getTimeInMillis() - time.getTime())/3600000); if(hour == 0) ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000,1)+"分钟前"; else ftime = hour+"小时前"; } else if(days == 1){ ftime = "昨天"; } else if(days == 2){ ftime = "前天"; } else if(days > 2 && days <= 10){ ftime = days+"天前"; } else if(days > 10){ ftime = dateFormater2.get().format(time); } return ftime; } /** * 判断给定字符串时间是否为今日 * @param sdate * @return boolean */ public static boolean isToday(String sdate){ boolean b = false; Date time = toDate(sdate); Date today = new Date(); if(time != null){ String nowDate = dateFormater2.get().format(today); String timeDate = dateFormater2.get().format(time); if(nowDate.equals(timeDate)){ b = true; } } return b; } /** * 判断是不是一个合法的电子邮件地址 * @param email * @return */ public static boolean isEmail(String email){ if(email == null || email.trim().length()==0) return false; return emailer.matcher(email).matches(); } /** * 字符串转整数 * @param str * @param defValue * @return */ public static int toInt(String str, int defValue) { try{ return Integer.parseInt(str); }catch(Exception e){} return defValue; } /** * 对象转整数 * @param obj * @return 转换异常返回 0 */ public static int toInt(Object obj) { if(obj==null) return 0; return toInt(obj.toString(),0); } /** * 对象转整数 * @param obj * @return 转换异常返回 0 */ public static long toLong(String obj) { try{ return Long.parseLong(obj); }catch(Exception e){} return 0; } /** * 字符串转布尔值 * @param b * @return 转换异常返回 false */ public static boolean toBool(String b) { try{ return Boolean.parseBoolean(b); }catch(Exception e){} return false; } }