3

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.

Marina
  • 3,222
  • 5
  • 25
  • 35
  • `getDate()` is [deprecated](http://developer.android.com/reference/java/util/Date.html#getDay()) so it might gives you some problems – mt0s Aug 10 '14 at 20:12
  • @mt0s, sorry I was being unclear: getDate() is a method I implemented, but essentially, it will lead to new Date() being called. I've made the change in my question to be more clear. thanks! – Marina Aug 10 '14 at 20:21
  • http://stackoverflow.com/a/25215560/1848929 – hakki Aug 10 '14 at 20:25
  • 2
    @hakiko that doesn't answer my question. My question is specifically about getting getLongDateFormat() to work as documented. – Marina Aug 10 '14 at 20:27
  • @Marina can you test to return dateformat instead, I think that Dateformat is formatted inn its toString method – Rod_Algonquin Aug 10 '14 at 20:31
  • Query long date format string and add "EEEE " to it, if it doesn't have it already. – cyanide Aug 11 '14 at 04:45
  • 1
    @Marina Did you ever figure out why the getLongDateFormat of android.text.format.DateFormat does not actually do what is advertised in the android reference guide? I see all of the answers here just provided a workaround for the issue. – estebro Dec 27 '15 at 16:13
  • As of today, it still does what @Marina originally reported. – pbergson Jul 14 '17 at 20:06

4 Answers4

4
private String SetFormatDate(Date date) {
    java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL);
    return dateFormat.format(date);
}
Phil
  • 41
  • 1
2

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

reiti.net
  • 315
  • 2
  • 12
1

Also you can do like 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);
}
0
private String formatDate(Date date)  
{  
    return DateFormat.format("EEEE, dd MMMM yyyy", date);  //import      android.text.format.DateFormat;   
}

Output : Tuesday, 27 October 2015

First argument is the format string and the second one is the current date. You can change the format of the date by changing the first argument i.e. format string. Check out the symbols in the Symbol Column and also the variations in the Example Column in the picture below.

See this image for format strings reference reference

For more format strings symbols check out http://developer.android.com/reference/java/text/SimpleDateFormat.html

Hope this helped

KrzyShzy
  • 156
  • 7