Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to use the android.text.format.DateFormat.format method

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!

like image 314
xBawbx Avatar asked Oct 21 '22 14:10

xBawbx


1 Answers

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.

like image 57
laalto Avatar answered Oct 23 '22 05:10

laalto