使用 SimpleDateFormat 报错 Exception in thread :java.lang.NumberFormatException: For input string: ""

public class TestSimpleDateFormat {

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date parse(String date) throws ParseException {
        return DATE_FORMAT.parse(date);
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        for (int i = 0; i < 500; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000000; i++) {
                        try {
                            DATE_FORMAT.parse("2014-01-01 00:00:00");
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        Thread.sleep(3000000);
    }
}

报错  Exception in thread :java.lang.NumberFormatException: For input string: ""

参考:https://www.cnblogs.com/zuoxiaolong/p/con1.html

      https://blog.csdn.net/Mrs_chens/article/details/90166399

 分析

 翻译过来就是:日期格式化的类是非同步的,建议为每一个线程创建独立的格式化实例。如果多个线程并发访问同一个格式化实例,就必须在外部添加同步机制。

解决办法一     加入synchronized 关键字  但是控制台 还会隐约有报错的信息闪过

public class TestSimpleDateFormat {

    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date parse(String strDate) throws ParseException {

        synchronized (DATE_FORMAT) {

            return DATE_FORMAT.parse(strDate);

        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        for (int i = 0; i < 500; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000000; i++) {
                        try {
                            System.out.println(DATE_FORMAT.parse("2019-05-13 11:30:59"));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        Thread.sleep(3000000);
    }
}

解决办法二      使用java8  DateTimeFormatter (推荐使用)

public class TestSimpleDateFormat {

    private static final DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDateTime parse2(String dateNow) {

        return LocalDateTime.parse(dateNow, sdf);

    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        for (int i = 0; i < 500; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000000; i++) {
                        System.out.println(sdf.parse("2019-05-13 11:30:59"));
                    }
                }
            });
        }
        Thread.sleep(3000000);
    }
}

猜你喜欢

转载自www.cnblogs.com/wf-zhang/p/12133728.html