I was reading the documentation for the android.text.format.DateFormat class on the Android developers website (located at http://developer.android.com/reference/android/text/format/DateFormat.html) in order to learn more about it. Specifically I am trying to format a date to be human readable so I found the format(CharSequence inFormat, Date inDate) method and thought that I would use this. Now most examples that I have been able to find for using this method look similar to this: android.text.format.DateFormat.format("MM/dd/yyyy", new java.util.Date());
However the documentation states:
Most callers should avoid supplying their own format strings to this class' format methods and rely on the correctly localized ones supplied by the system.
It is from this line that my confusion and questions arise.
1.) The documentation does not appear to state (at least I was unable to find it anywhere despite my hardest searching) how to access the correctly localized strings that it says that the system provides. How is this accomplished?
2.) Is it acceptable (despite what the documentation says) to provide your own format string as in the example line above?
Thanks in advance for your help!
1.) The documentation does not appear to state (at least I was unable to find it anywhere despite my hardest searching) how to access the correctly localized strings that it says that the system provides. How is this accomplished?
The most straightforward way is to use one of the static get..Instance()
methods such as getDateTimeInstance()
to obtain a java.text.DateFormat
object. The returned formatter will be set up to be appropriate for the current system locale.
Use format()
on the formatter object to obtain the formatted strings.
Using a SimpleDateFormat
is often also easy enough.
2.) Is it acceptable (despite what the documentation says) to provide your own format string as in the example line above?
When you are formatting for computers to read, yes, for example when serializing to an ASCII based serialization format that requires a certain date format.
When formatting for humans to read, use the appropriate locale defaults.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With