2

I trying to find out is it thread safe to use

 DateFormat dateFormat =android.text.format.DateFormat.getDateFormat(getApplicationContext());

I'm well aware that object DateFormat is not thread safe, but the previous snippet seems quite popular way for writing android programs e.g. How do you format date and time in Android?, and I haven't noticed any mention of the threading issue.

Community
  • 1
  • 1
bbaja42
  • 2,099
  • 18
  • 34
  • possible duplicate of [Synchronizing on SimpleDateFormat vs. clone](http://stackoverflow.com/questions/5038169/synchronizing-on-simpledateformat-vs-clone) – Peter Knego Dec 25 '11 at 20:11
  • @Peter Bear in mind that returned method values is DateFormat, not SimpleDate format. If you can show that the implementation is indeed SimpleDateFormat, I consider the topic resolved. – bbaja42 Dec 25 '11 at 20:25
  • You're right. My mistake. Posted an answer below. – Peter Knego Dec 25 '11 at 21:06

1 Answers1

2

android.text.format.DateFormat is a mix of it's own functionality and java.text.SimpleDateFormat. See for yourself: http://www.google.com/codesearch#cZwlSNS7aEw/frameworks/base/core/java/android/text/format/DateFormat.java

As far as I see, it's format(..) methods do not use SimpleDateFormat nor does it use any instance fields (just some static fields are read), so they should be thread-safe.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 2
    I'll accept this answer as correct one, since source code gives all the necessary information. However, I find the resulting behaviour worrisome, on one hand `format` methods are thread safe; but the `getDateFormat` returns new instance of `SimpleDateFormat` which is not thread safe. The java doc information doesn't event mention threading. – bbaja42 Dec 25 '11 at 21:21