SimpleDateFormat in Java isn't thread-safe use carefully

Source: Internet
Author: User
Tags dateformat parse string

SimpleDateFormat In JavaVery 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 is not thread-safe and buggy. Call to Format () and parse () method mutate state of DateFormat class and should is synchronized externally in order to AVO ID any issue. Here is few points which you should take care while using SimpleDateFormat in Java:
SimpleDateFormat in Java are not Thread-safe1) Use local dateformat or simpledateformat objects to converting or formatting dates in Java. Making them local ensure that they is not being shared between multiple threads. 2) If you are sharing date& Nbsp;for simpledateformat class in Java and need to externally synchronizecall To format ()  and  parse ()  method as they mutate state of dateformat object and can create subtle and hardto fix bugs WH Ile formatting Strings or creating dates in Java. Best are to avoid sharing of dateformat class altogether. 3) If you have option Use joda date Time Libr Ary 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.OutOfM Emoryerror if not used carefully. So avoid until you don ' t has any other option. that's all on  SimpleDateFormat In Java. Many people has pointed off flaw on design of Format class but unfortunately Sun or Oracle have not addressed them in any R Elease not even in JDK7. SimpleDateFormat is still first choice because of it availability on the standard library but should be used carefully.



Read MORE:HTTP://JAVAREVISITED.BLOGSPOT.COM/2012/03/SIMPLEDATEFORMAT-IN-JAVA-IS-NOT-THREAD.HTML#IXZZ3QDZXPBBQ

SimpleDateFormat in Java isn't thread-safe use carefully

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.