javaapache-commons-lang

How to use FastDateFormat to create Date object using String


I used SimpleDataFormat and it seems to be not a very thread safe thing to use. Then I found that FastDateFormat is the alternative to this problem. But I'm trying to figure out how to use it in order to parse String with Date and Time.

Here is a explanation on why wrong use of SimpleDateFormat is bad : Why is Java's SimpleDateFormat not thread-safe?

Problem in code : I have following string

String date = "2014-04-27'T'13:45:31";

I need to convert that into Date. But in a thread safe way. I would like to use something related to FastDateFormat. Not the SimpleDateFormat


Solution

  • Parsing is supported since commons-lang 3.2.

    The basic recipe:

    1. Create a FastDateFormat instance using the static method suiting your specific needs.
    2. Use this to parse the string and obtain a Java Date object.

    That's it.