李振海
2017-12-14 12:50:58
SimpleDateFormat线程安全或java.lang.NumberFormatException: multiple points异常解决
公司服务器一直报
java.lang.NumberFormatException: multiple points at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.text.DigitList.getDouble(DigitList.java:169) at java.text.DecimalFormat.parse(DecimalFormat.java:2056) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at cn.xjwlfw.yeshenghuo.system.util.DateUtil.parseDate(DateUtil.java:39) 或者 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2051) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at cn.xjwlfw.yeshenghuo.system.util.DateUtil.parseDate(DateUtil.java:39)
等等错误,
之前一直以为数据转换异常,后来查到多线程下SimpleDateFormat 是不安全的
之前的工具类
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); /** * 获取当前时间 * * @return 当前时间格式化后的字符串 示例: "2000-10-0 18:00:00" */ public static String getCurrentTime() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } /** * 获取当前格式化时间 * * @param pattern - 时间格式 示例: "yyyy-MM-dd HH:mm:ss" * @return 当前时间格式化后的字符串 示例: "2000-10-0 18:00:00" */ public static String getCurrentTime(String pattern) { return new SimpleDateFormat(pattern).format(new Date()); } public static String formatDate(String pattern, Date date) { simpleDateFormat.applyPattern(pattern); return simpleDateFormat.format(date); } public static Date parseDate(String pattern, String dateStr) throws ParseException { simpleDateFormat.applyPattern(pattern); return simpleDateFormat.parse(dateStr); }
多线程环境下测试
测试代码
final String patten1 = "yyyy-MM-dd"; final String patten2 = "yyyy-MM"; Thread t1 = new Thread() { @Override public void run() { try { for(int i=0;i<20000;i++){ System.out.println("线程1:"+i); DateUtil.simpleDateparseDate( patten1,"1992-09-13"); } } catch (Exception e) { e.printStackTrace(); } } }; Thread t2 = new Thread() { @Override public void run() { try { for(int i=0;i<200000;i++){ System.out.println("线程2:"+i); DateUtil.simpleDateparseDate( patten1,"1992-09-13"); } } catch (Exception e) { e.printStackTrace(); } } }; t1.start(); t2.start(); 修改后的工具类 //private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>(); /** * 获取当前时间 * * @return 当前时间格式化后的字符串 示例: "2000-10-0 18:00:00" */ public static String getCurrentTime() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } /** * 获取当前格式化时间 * * @param pattern - 时间格式 示例: "yyyy-MM-dd HH:mm:ss" * @return 当前时间格式化后的字符串 示例: "2000-10-0 18:00:00" * * 解决方案1 * */ public static String getCurrentTime(String pattern) { return new SimpleDateFormat(pattern).format(new Date()); } public static String simpleDateformatDate(String pattern, Date date) { return new SimpleDateFormat(pattern).format(date); } /** * * * * 解决方案2 * */ public static Date simpleDateparseDate(String pattern, String dateStr) throws ParseException { return new SimpleDateFormat(pattern).parse(dateStr); } public static Date parseDate(String pattern,String str) throws ParseException { SimpleDateFormat sdf = local.get(); if (sdf == null) { sdf = new SimpleDateFormat(pattern, Locale.US); local.set(sdf); } try { return sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return new SimpleDateFormat(pattern).parse(str); } public static String formatDate(String pattern,Date date){ SimpleDateFormat sdf = local.get(); if (sdf == null) { sdf = new SimpleDateFormat(pattern, Locale.US); local.set(sdf); } return sdf.format(date); } 附上几篇博客 https://www.cnblogs.com/zemliu/archive/2013/08/29/3290585.html http://blog.csdn.net/zdp072/article/details/41044059
评论