Chapter 9 Challenge Solution


In the CrimeListFragment class, make the following change to the bind() method:

public void bind(Crime crime) {
mCrime = crime;
mDateTextView.setText(DateFormat.format(“EEEE, MMM dd, yyyy”, mCrime.getDate()));
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);

This table is helpful in creating the formatting string


I am getting this error, non static method (format) cannot be referenced from static method.


crime.getDate() not mCrime.getDate()


When use “EEEE, MMM dd, yyyy” you hard fixing the order of month name and date: month, then day.
But for many countries it is not true! Many locales supposed to use format “21 Mar 2018” not “Mar 21, 2018”
Also, user can change 12/24 time format independently of system language. For example, I can use English (United States) language for UI, but set 24 time format in Time&Date settings.

My solution:

DateFormat dateDateFormat = android.text.format.DateFormat.getMediumDateFormat(getActivity());
DateFormat timeDateFormat = android.text.format.DateFormat.getTimeFormat(getActivity());
Date date = crime.getDate();
String dateString = String.format("%s, %s %s",
        android.text.format.DateFormat.format("EEEE", date),


I did with the use of Calendar class.

Inside public class CrimeListFragment:

 Calendar myCalendar = Calendar.getInstance();
    String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(myCalendar.getTime());

//now apply changes to bind() method

public void bind(Crime crime){


it works great but can you explain how you came up with this code


Wanted to note that the android documentation recommends not sending in your own format, but if you do, it suggests using this method:

getBestDateTimePattern(Locale locale, String skeleton)

The Locale class contains various static locales that can be passed in such as Locale.US or Locale.UK and it will adjust your pattern accordingly. Here’s how I implemented it:

private String formatDate(Date date, Locale locale) {
    String df = DateFormat.getBestDateTimePattern(locale, "EEEE, MMM dd, yyyy");
    return DateFormat.format(df, date).toString();

And I call that method in the CrimeHolder bind method like:

mDateTextView.setText(formatDate(crime.getDate(), Locale.US));