Marina Marina - 1 year ago 136
Android Question

Date Format's getLongDateFormat() isn't displaying the day of the week

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 ( Am I using getLongDateFormat() incorrectly?

I'm using Android API Level 19 as my target.

Answer Source

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.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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download