1

The set of date and time functions I need are pretty basic.

  1. An object to represent a date/time (for convenience in function calls).
  2. Conversion functions to and from y,m,d,h,m,s.
  3. Format/parse functions to and from numeric-only localised string representation. Eg dd/mm/yyyy, yyyy-mm-dd, mm.dd.yyyy or whatever order and delimiters are locally expected.
  4. A system function to get the current local date and time (timezones not required).
  5. Compatible with the DatePicker widget.
  6. Thread safe. Static functions available to both UI and worker (NDK) threads.

So far I've found that:

  • Calendar and GregorianCalendar can do the conversions, but they're clunky to use and they're not thread safe.
  • SimpleDateFormat can do the formatting, if I could only figure out which magic string to feed it! The default is not numeric.
  • Time has a nicer set of conversion functions, but has no date/time object and the parse/format functions are documented only indirectly. And it smells a lot more like Unix than Java.

So do I find a way to fix the thread safety and try to persuade SimpleDateFormat to give me what I need? Or do I give up and jump ship to Time? Or have I missed something?


Just to be clear, this is not a request for a library recommendation or a shopping list. It's a request for assistance on how to implement a specific set of functions using the given Android API. I'm looking for an expert on using these libraries to point out a path through the morass. I would hope that a well-written answer would benefit other readers also struggling with this part of Android.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • 1
    The moderators on StackOverflow are allergic to any discussions of recommending or comparing libraries. See this new StackExchange, [Software Recommendations](http://softwarerecs.stackexchange.com/). – Basil Bourque May 04 '14 at 16:59
  • Possible duplicated question, see Answer for this question [in this thread](http://stackoverflow.com/a/6840897/3187366) – issamux May 04 '14 at 18:07
  • @issamux: Not a duplicate, but some helpful hints on how to tackle the threading problem. Thanks. – david.pfx May 05 '14 at 05:11
  • While I agree, this shouldn't be a "use this library or that one" question, in this case the standard Java APIs (which Android is based on) for manipulating Date and Time are just simply inadequate. That's why the creator of Joda Time created the new date management classes for Java 8 (http://java.dzone.com/articles/introducing-new-date-and-time) – Jason Lowenthal May 05 '14 at 13:04

1 Answers1

1

Personally I find Joda-Time to be able to handle almost everything you need for date and time. Since it's just a .jar it should be able to be imported.

Here's how to use it, for the questions you asked specifically:

  1. An object to represent a date/time - DateTime for immutable or MutableDateTime for one that you need to apply transformations to.
  2. These objects have many methods for conversion, see the Joda-Time API for AbstractDateTime as an example, as all of Joda's classes extend AbstractDateTime
  3. You can use String convertedToString = new DateTime().toString("yyyy-MM-dd") to get it as 2014-05-05. To reverse this, use this API: DateTimeFormatter parseDateTime(someStringThatRepresentsADate)
  4. DateTime now = new DateTime() gives you now, as a DateTime object
  5. You can get a DateTime object from several other classes, such as Calendar by doing something like DateTime fromCalendar = new DateTime(myCalendarObject)
  6. See The FAQ for Joda on multi-threading

In order to import Joda into your Android project, assuming you're using Android studio and Gradle, see this answer: Android Studio: Add jar as library?

Community
  • 1
  • 1
Jason Lowenthal
  • 790
  • 1
  • 5
  • 16
  • Agreed. The java.util.Date, .Calendar, and SimpleDateFormat classes bundled with Java (and Android) are notoriously troublesome. Avoid them. Use Joda-Time, or in Java 8 (but not Android currently) the new [java.time package](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html). – Basil Bourque May 04 '14 at 16:54
  • Thanks for the suggestion. At a quick look, Joda time is massive overkill for what I need, and some of the features I am looking for I could not find in the documentation. If you do think this answers my question, please point out the API functions I should use (and also how to use it on Android). – david.pfx May 05 '14 at 03:48
  • I updated my answer to match more closely with how you framed your question. Let me know if you want any additional information. – Jason Lowenthal May 05 '14 at 13:00