使用 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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM