Date format getLongDateFormat () does not display the day of the week

I created a private method to get the date and formatted it to look like "Sunday 10 Aug 2014":

private String formatDate(Date date){
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getLongDateFormat(getActivity().getApplication());
        return dateFormat.format(date);
    }

      

In the OnCreateView method of my fragment, I use this method to set the button text:

mDateButton.setText(formatDate(new Date()));

      

When I launch my app, it just says "Aug 10, 2014". It doesn't make sense to me as the Android documentation says getLongDateFormat () should show the day of the week ( http://developer.android.com/reference/android/text/format/DateFormat.html#getLongDateFormat(android.content .Context) ) Am I using getLongDateFormat () wrong?

I am using Android API level 19 as my target.

+3


source to share


4 answers


I had the same problem, it just displayed the date (no weekday). I found another way to get the corresponding 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);

      



This is a static function and you can easily determine what parts you want in your date string. The view is localized.

see http://developer.android.com/reference/android/text/format/DateUtils.html

+2


source


private String SetFormatDate(Date date) {
    java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL);
    return dateFormat.format(date);
}

      



+4


source


Also you can do this

private String formatDate(Date date){
    java.text.DateFormat df = android.text.format.DateFormat.getLongDateFormat(getActivity().getApplicationContext());
    return android.text.format.DateFormat.format("EEEE", date) + ", " + df.format(date);
}

      

+1


source


private String formatDate(Date date)  
{  
    return DateFormat.format("EEEE, dd MMMM yyyy", date);  //import      android.text.format.DateFormat;   
}

      

Output: Tuesday, 27 October 2015

The first argument is a format string and the second is the current date. You can change the date format by changing the first argument, i.e. Format string. Check the symbols in the Symbol column and the options in the Column example in the figure below.

See this image for a link to format strings reference

For larger format characters, the characters are http://developer.android.com/reference/java/text/SimpleDateFormat.html

Hope this helped

0


source







All Articles