The set of date and time functions I need are pretty basic.
- An object to represent a date/time (for convenience in function calls).
- Conversion functions to and from y,m,d,h,m,s.
- 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.
- A system function to get the current local date and time (timezones not required).
- Compatible with the DatePicker widget.
- Thread safe. Static functions available to both UI and worker (NDK) threads.
So far I've found that:
Calendar
andGregorianCalendar
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.