4

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

Community
  • 1
  • 1
dinesh707
  • 10,694
  • 20
  • 74
  • 121
  • 2
    SimpleDateFormat is threadsafe if a new instance is created each time a conversion needs to be done. – sp00m Aug 27 '14 at 12:14
  • agree, But like to know how to use FastDateFormat as well. – dinesh707 Aug 27 '14 at 12:14
  • What is your concrete problem? The basic recipe: - Create a FastDateFormat instance using the static method suiting your specific needs. - Use this to parse the string and obtain a Java Date object. That's it. Where are you running into issues? – Ray Aug 27 '14 at 12:21
  • 2
    From the JavaDoc you linked: `Only formatting is supported ...` so there is no way to parse a String into a Date object. – Thomas Aug 27 '14 at 12:22
  • 2
    Parsing is supported since commons-lang 3.2. – Ray Aug 27 '14 at 12:22
  • 1
    Another alternative is to use Joda Time instead, which has thread-safe formatters... and is a cleaner date/time API to start with. – Jon Skeet Aug 27 '14 at 12:24
  • Thank you @Ray. I was using 3.0. When i changed it to 3.2 it can be used similar to the SimpleDateFormat. If you answer that I'll accept it. Thank you all for help – dinesh707 Aug 27 '14 at 12:27

1 Answers1

4

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.

Ross
  • 985
  • 15
  • 32
Ray
  • 2,574
  • 1
  • 14
  • 27