SimpleDateFormat in Java is not Thread-Safe Use Carefully

简介: SimpleDateFormat in Java  very common and used to format Date to String and parse String into Date in Java but it can cause very subtle and hard to d...
SimpleDateFormat  in Java  very common and used to  format Date to String and  parse String into Date in Java but it can cause very subtle and hard to debug issues if not used carefully because DateFormat and SimpleDateFormat both are not thread-safe and buggy. call to format() and parse() method mutate state of DateFormat class and should be  synchronized externally in order to avoid any issue. here are few points which you should take care while using SimpleDateFormat in Java:

SimpleDateFormat in Java is not Thread-Safe

1) Use local DateFormat or SimpleDateFormat objects for converting or formatting dates in Java. Making them local ensure that they will not be shared between multiple  Threads.
 
2) If you are sharing Date for SimpleDateFormat class in Java then you need to externally synchronize
call to format() and parse() method as they mutate state of DateFormat object and can create subtle and hard
to fix bugs while formatting Strings or creating dates in Java. Best is to avoid sharing of DateFormat class altogether.
 
3) If you have option use  JODA date time library for your date and time related operation. its easy to understand and portable with Java Date API and solves all  thread-safety issues associated with SimpleDateFormat in Java.
 
4) Another good alternative of SimpleDateFormat in Java is Apaches' commons.lang package which hold a class called  FastDateFormat utility class and thread-safe alternative of SimpleDateFormat in Java.
 
5) Another approach of synchronizing DateFormat and SimpleDateFormat is using ThreadLocal, which create SimpleDateFormat on per Thread basis but it can be source of severe  memory leak and java.lang.OutOfMemoryError if not used carefully. so avoid until you don't have any other option.
 
That’s all on  SimpleDateFormat  in Java. Many people has pointed out flaw on design of Format class but unfortunately Sun or Oracle has not addressed them in any release not even in JDK7. SimpleDateFormat is still first choice because of its availability on standard library but should be used carefully.



Read more: http://javarevisited.blogspot.com/2012/03/simpledateformat-in-java-is-not-thread.html#ixzz3QDZXPBbq

目录
相关文章
|
7月前
|
Java Windows
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
windows下 安装 Elasticsearch报错warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME
469 0
|
28天前
|
安全 Java API
告别SimpleDateFormat:Java 8日期时间API的最佳实践
在Java开发中,处理日期和时间是一个基本而重要的任务。传统的`SimpleDateFormat`类因其简单易用而被广泛采用,但它存在一些潜在的问题,尤其是在多线程环境下。本文将探讨`SimpleDateFormat`的局限性,并介绍Java 8引入的新的日期时间API,以及如何使用这些新工具来避免潜在的风险。
34 5
|
3月前
|
安全 Java API
时间日期API(Date,SimpleDateFormat,Calendar)+java8新增日期API (LocalTime,LocalDate,LocalDateTime)
这篇文章介绍了Java中处理日期和时间的API,包括旧的日期API(Date、SimpleDateFormat、Calendar)和Java 8引入的新日期API(LocalTime、LocalDate、LocalDateTime)。文章详细解释了这些类/接口的方法和用途,并通过代码示例展示了如何使用它们。此外,还讨论了新旧API的区别,新API的不可变性和线程安全性,以及它们提供的操作日期时间的灵活性和简洁性。
|
4月前
|
安全 Java
java系列知识之~SimpleDateFormat日期格式化
这篇文章介绍了Java中`SimpleDateFormat`类的使用,包括如何创建实例、格式化日期对象为字符串、解析字符串为日期对象,并提供了常见日期模式和使用示例,同时指出了`SimpleDateFormat`不是线程安全及其它一些注意事项。
|
6月前
|
存储 安全 Java
java编程SimpleDateFormat详解
java编程SimpleDateFormat详解
|
6月前
|
安全 Java 测试技术
滚雪球学Java(53):从入门到精通:SimpleDateFormat类高深用法,让你的代码更简洁!
【6月更文挑战第7天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
50 0
滚雪球学Java(53):从入门到精通:SimpleDateFormat类高深用法,让你的代码更简洁!
|
6月前
|
Java
springboot 异常java.net.BindException: Address already in use: bind
springboot 异常java.net.BindException: Address already in use: bind
79 0
|
6月前
|
Java
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
75 0
|
Java
JAVA 端口被占用 报错解决方案:java.net.BindException: Address already in use: bind
JAVA 端口被占用 报错解决方案:java.net.BindException: Address already in use: bind
249 0
|
7月前
|
Java
Java中SimpleDateFormat的具体用法
Java中SimpleDateFormat的具体用法
58 0