61

Is the Joda-Time DateTimeFormatter class thread safe? Once I get an instance from DateTimeFormat.forPattern, can its various parse methods be called by multiple threads? DateTimeFormatter's Javadocs makes no mention of thread safety.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
Steve Kuo
  • 58,491
  • 75
  • 189
  • 247

3 Answers3

83

Yes, it is:

DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

and so is the Java 8 version

Implementation Requirements: This class is immutable and thread-safe.

Sled
  • 16,514
  • 22
  • 110
  • 148
Erre Efe
  • 14,878
  • 10
  • 41
  • 73
  • 3
    And so is the Java8 one as well. See https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – muttonUp Mar 04 '15 at 20:13
13

A quick look at the code shows there isn't any mutable shared state in DateTimeFormatter, which would make it thread safe.

Mark Elliot
  • 68,728
  • 18
  • 135
  • 157
  • 11
    Nitpick: A quick look at the code does not necessarily show how the code will be changed in the next point release. It's always better to have an explicit guarantee in the documented API (of course, additionally checking the code cannot hurt). – sleske Dec 14 '15 at 14:21
3

Found this question on top of google's answers when checking for Java's java.time.format.DateTimeFormatter thread safety

Java's own DateTimeFormatter is also thread-safe, as the documentation states:

This class is immutable and thread-safe

Dariusz
  • 19,750
  • 7
  • 65
  • 104