SimpleDateFormat线程不安全的日期格式化库

SimpleDateFormatJAVA提供的一个日期转换类。

package com.rumenz.task;import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;//线程不安全
public class DataFormat {private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");private static Integer clientTotal=5000;private static Integer threadTotal=200;public static void main(String[] args) throws Exception{ExecutorService executorService = Executors.newCachedThreadPool();final Semaphore semaphore=new Semaphore(threadTotal);final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);for (int i = 0; i <clientTotal ; i++) {executorService.execute(()->{try{semaphore.acquire();update();semaphore.release();}catch (Exception e){e.printStackTrace();}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();}private static void update() {try{simpleDateFormat.parse("20200115");}catch (Exception e){e.printStackTrace();}}
}

运行的时候回报错,线程不安全

java.lang.NumberFormatException: For input string: "E152"at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)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:2089)at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1867)at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)at java.text.DateFormat.parse(DateFormat.java:364)at com.rumenz.task.DataFormat.update(DataFormat.java:47)at com.rumenz.task.DataFormat.lambda$main$0(DataFormat.java:30)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)

如果要使用SimpleDateFormat可以将其做成局部变量,这样在多线程环境下就不会出现线程安全问题。或者使用synchronized给方法加锁。

jodaTime 线程安全的格式化库

引入依赖

<dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.9.9</version>
</dependency>
package com.keytech.task;import org.joda.time.DateTime;import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;//线程安全
public class DataFormat1 {public static Integer clientTotal=5000;public static Integer threadTotal=200;private static DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyyMMdd");public static void main(String[] args)  throws Exception{ExecutorService executorService = Executors.newCachedThreadPool();final Semaphore semaphore=new Semaphore(threadTotal);final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);for (int i = 0; i < clientTotal; i++) {executorService.execute(()->{try{semaphore.acquire();update();semaphore.release();}catch (Exception e){e.printStackTrace();}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();}private static void update() {DateTime dateTime = DateTime.parse("20200115").toDateTime();System.out.println(dateTime);}
}

DateTimeFormatter JAVA8中线程安全的日期转换类

package com.keytech.task;import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;//线程安全
public class DateFormat2 {public static Integer clientTotal=5000;public static Integer threadTotal=200;private  static final  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");public static void main(String[] args) throws Exception{ExecutorService executorService = Executors.newCachedThreadPool();final Semaphore semaphore=new Semaphore(threadTotal);final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);for (int i = 0; i < clientTotal; i++) {try{semaphore.acquire();update();semaphore.release();}catch (Exception e){e.printStackTrace();}countDownLatch.countDown();}countDownLatch.await();executorService.shutdown();}private static void update() {LocalDate localDate = LocalDate.parse("20200115", formatter);System.out.println(localDate);}
}

JAVA8中自带,直接可以使用,不需要引入其它的库。

关注微信公众号:【入门小站】,解锁更多知识点

JAVA日期安全格式化之SimpleDateFormat和jodaTime,DateTimeFormatter相关推荐

  1. java日期格式大全 format SimpleDateFormat

    http://eternal1025.iteye.com/blog/344360 http://www.freejsp.net/Spring/13325.htm 1.日期型 转换为 String Da ...

  2. 【Java 基础】字符串(String、StringBuilder),日期(Date、SimpleDateFormat、Calendar)

    字符串.日期 字符串(String) 字符串常量池(String Constant Pool) 字符串的初始化 intern 方法 字符串的常用方法(截取) 可变字符串(StringBuilder) ...

  3. java yyyy-mm-dd 日期格式_Java中的日期时间格式化

    原标题:Java中的日期时间格式化 1 Java日期时间格式化的概念 我们在日常的开发过程中常常会碰到关于日期时间的计算与存储问题,比如我们要把一个当前时间类型转换成字符串类型,我们会直接使用Util ...

  4. dateformat 返回类型_详解Java中格式化日期的DateFormat与SimpleDateFormat类

    DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...

  5. Java日期格式化(DateFormat类和SimpleDateFormat类)

    格式化日期表示将日期/时间格式转换为预先定义的日期/时间格式.例如将日期"Fri May 18 15:46:24 CST2016" 格式转换为 "2016-5-18 15 ...

  6. java 日期格式化 英文_Java SimpleDateFormat 中英文时间格式化转换

    SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析(文本 -> 日期)和规范化. SimpleDateFor ...

  7. Java日期相关类:Date、SimpleDateFormat和Calendar类常用API代码示例

    Date: 表示特定的瞬间,精确到毫秒,他可以通过方法来设定自己所表示的时间,可以表示任意的时间 Date类的构造方法 Date() :创建的是一个表示当前系统时间的Date对象 Date(long ...

  8. java日期格式精确到分_详解Java日期格式化及其使用例子

    Java日期格式化及其使用例子 1 SimpleDateFormat担当重任,怎样格式化都行 import java.util.Date; import java.text.SimpleDateFor ...

  9. java日期格式化yyyy-mm-dd

    在 Java 中,你可以使用 SimpleDateFormat 类来格式化日期.以 "yyyy-MM-dd" 为例,你可以这样写: SimpleDateFormat dateFor ...

最新文章

  1. python deque双端队列的神奇用法
  2. RHEL6入门系列之九,常用命令2
  3. Google学术搜索方法
  4. Selenium - 简介
  5. PHP包含文件函数include、include_once、require、require_once区别
  6. macOS下配置环境变量/查看环境变量
  7. 深度学习修炼(一)——从机器学习转向深度学习
  8. css 可编辑,如何设置DIV可编辑
  9. J2EE常用Listener(转载)
  10. 我的FPGA入门学习计划
  11. C言语教程第五章:函数(4)
  12. 什么是开放源代码,以及为什么不使用开放源代码?
  13. NUCLEO开发板:STM32 st-link驱动程序错误
  14. 客户下单邮件跟进书写
  15. python画两条曲线_python 实现将多条曲线画在一幅图上的方法
  16. 致远oa mysql 安装_致远OA协同办公系统OA安装步骤.doc
  17. photoshop调人像冷色
  18. 深入理解Zend执行引擎
  19. 【JAVA长虹键法】第一式 初识设计模式(23种设计模式)
  20. jfinal整合IJPay paypal支付

热门文章

  1. vue 使用axios发送的请求使用md5加密
  2. Ubuntu16.04安装最新版nodejs
  3. TCP、UDP绑定同一端口通信的解释-转
  4. MyBatis 一级缓存二级缓存详解
  5. 1095 解码PAT准考证 (25 point(s)) - PAT乙级真题
  6. L1-054 福到了-PAT团体程序设计天梯赛GPLT
  7. InfluxDB简介,InfluxDB的基本操作
  8. Hbase完全分布式集群安装配置(Hbase1.0.0,Hadoop2.6.0)
  9. 带你通俗理解https
  10. linux部署tomcat8(基于centOS7)