I've created a private method to receive a Date, and format it so it looks like "Sunday, August 10, 2014":
private String formatDate(Date date){
java.text.DateFormat dateFormat = android.text.format.DateFormat.getLongDateFormat(getActivity().getApplication());
return dateFormat.format(date);
}
In my fragment's OnCreateView method, I use that method to set a button's text:
mDateButton.setText(formatDate(new Date()));
When I run my app, however, it just says "August 10, 2014." This doesn't make sense to me, since the Android documentation says that getLongDateFormat() should be displaying the day of the week (http://developer.android.com/reference/android/text/format/DateFormat.html#getLongDateFormat(android.content.Context)). Am I using getLongDateFormat() incorrectly?
I'm using Android API Level 19 as my target.
I just had the same problem, it just displayed the date (without weekday). I've found another method of getting the appropriate date string:
android.text.format.DateUtils.formatDateTime(getApplicationContext(),
millis,
android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY | android.text.format.DateUtils.FORMAT_SHOW_DATE | android.text.format.DateUtils.FORMAT_SHOW_YEAR);
It's a static function and you can easily define which parts you want to have in your date string. The representation is localized.
see http://developer.android.com/reference/android/text/format/DateUtils.html