17

I read some posts on formatting date on android, like this:

How do you format date and time in Android?

People suggest use android.text.format.DateFormat rather than java.text.DateFormat,

Also here, it mention a problem when converting date to string in android:

Android load timezone too long: Loaded time zone names for en_US

I am wondering what's the difference between android.text.format.DateFormat and java.text.DateFormat?

Community
  • 1
  • 1
virsir
  • 15,159
  • 25
  • 75
  • 109

2 Answers2

18

As far as I can tell, android.text.format.DateFormat has some of the functionality from java.text.DateFormat, some of the functionality from java.text.SimpleDateFormat, and some extra functionality of its own.

Most notably:

  • The java SimpleDateFormat allows construction of arbitrary non-localized formats.
  • The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.
  • The android DateFormat allows most of the above (arbitrary formats and a smaller number of localized formats), but also provides getBestDateTimePattern which picks a locale-appropriate format string that contains the elements specified with locale-appropriate ordering and punctuation.

So, if you need a localized date/time format other than the three provided by java's DateFormat class, the android DateFormat class is the solution.

Less importantly, but an additional convenience: the android DateFormat methods can take a Calendar or long milliseconds directly, instead of requiring a Date object. I always prefer working with Calendar or long over Date. Also, it properly respects the timezone of the Calendar object -- whereas getting a Date from a Calendar and passing that along to the formatter loses the timezone information. (Nothing you can't get around via java DateFormat's setCalendar method, but it's nice to not have to.)

Finally, and least importantly, some of the methods of the Android DateFormat don't actually construct a formatter, you just construct a format string. All of this class's methods are static. The methods do that construct a DateFormat actually construct a java DateFormat!

benkc
  • 3,292
  • 1
  • 28
  • 37
  • 8
    This should be the accepted answer. It at least attempts to answer the question instead of snarkily linking to the online documentation. "Read the documentation" should not be an accepted answer ever imho. – akousmata May 08 '15 at 19:34
  • I'd be happy to delete my old, crappy answer, but SO does not let you delete the accepted answer. – Matt Ball Mar 22 '17 at 14:04
-19

How about some self-help here? Compare the APIs, read the JavaDocs and see for yourself:

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 18
    A wise man once told me: _"Give a man a fire, and he'll be warm for the rest of the night. Set a man on fire, and he'll be warm for the rest of his life."_ – Matt Ball Dec 16 '11 at 03:52
  • was this your answer from one of your early days? – Rohan Bhatia May 29 '17 at 16:12
  • actual quote is : "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." – Rohan Bhatia May 29 '17 at 16:40