Chapter 9 Challenge Solution

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

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
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

3 Likes

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

crime.getDate() not mCrime.getDate()

1 Like

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),
        dateDateFormat.format(date),
        timeDateFormat.format(date));
dateTextView.setText(dateString);
1 Like

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){
.....
 mDateTextView.setText(currentDate);
}

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));

I just changed the GETTER in Crime.java to return the converted string, and in the BIND method I removed the .toString() call.

In my view this is the best location for the date conversion, what do you think?

/**
* THIS GETTER HAS BEEN MODIFIED FOR THE CHALLENGE P 204, BY MAKING
THE DATA CONVERSION
* AND RETURNING THE STRING TO CRIME LIST FRAGMENT.JAVA
*/
public String getDate() {
String mDateView = DateFormat.getDateInstance(DateFormat.FULL).format(mDate);
return mDateView;
}

public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate());//CHALLENGE DATA from CRIME.JAVA
mSolvedImageView.setVisibility(crime.isSolved() ? View.VISIBLE : View.GONE);
}//method

My goal was to keep the bind method clean while getting the date format to read as “Thursday, Oct 11, 2018”. I ultimately opted for one line of code:

public void bind(Crime crime) {
        mCrime=crime;
        mTitleTextView.setText(mCrime.getTitle());
        mDateTextView.setText(new SimpleDateFormat("EEEE, MMM d, yyyy",Locale.getDefault()).format(crime.getDate()));
        mSolvedImageView.setVisibility(crime.isSolved()?View.VISIBLE:View.GONE);
    }

For a list of the date meanings/abbreviations (i.e. “EEEE, MMM” etc.) go to [https://developer.android.com/reference/java/time/format/DateTimeFormatter]
If you only use one letter for the day “E” it will give you “Thu” and “EEEE” will spell out the whole day “Thursday”. Play around with it.

This is a link for setting the Locale. I went with Locale.getDefault() to keep it dynamic.
[https://developer.android.com/reference/java/util/Locale]

I am interested in hearing other ideas.

4 Likes

Easy and simpleDateFormat