Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Working with JSON Formatting a Date

Derrick Shepherd
Derrick Shepherd
4,861 Points

Ok, I've Made Several Attempts At This, watched the video five times and still can't figure out how to convert the date.

Please Help :(

Movie.java
import java.util.Date;

public class Movie {

    private String mTitle;
    private Date mReleaseDate;

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public Date getReleaseDate() {
        return mReleaseDate;
    }

  public String getFormattedReleaseDate() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    formatter.setReleaseDate(ReleaseDate.getReleaseDate());
    Date date = new Date(getDate());
    String dateTime = formatter.format(date);
    return dateTime;
  }

    public void setReleaseDate(Date date) {
        mReleaseDate = date;
    }
}
Derrick Shepherd
Derrick Shepherd
4,861 Points

This Is The Question In This Code Challenge: "The mReleaseDate member variable is already stored as a Date object. Convert it and return it using the SimpleDateFormat variable."

3 Answers

Hi Derrick,

You've made the method, getFormattedReleaseDate, now that needs to amend the mReleaseDate member variable.

First, set up a formatter, as you have done correctly:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Then, use it on the mReleaseDate variable (and you might as well return that at the same time):

return format.format(mReleaseDate);

And that's it!

  public String getFormattedReleaseDate(){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    return format.format(mReleaseDate);
  }

I hope that helps.

Steve.

If you called your formatter something like dateFormatter or simpleFormatter that might be a little clearer. But that's being picky.

  public String getFormattedReleaseDate(){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return simpleDateFormat.format(mReleaseDate);
  }
Derrick Shepherd
Derrick Shepherd
4,861 Points

Thank You So Much Steve! It seems like once again I was overthinking the code challenge.

No problem! It is easy to overthink them sometimes. I guess if you try to think in language terms of what you're trying to achieve, and what you're working with, that might make the goal a little clearer?

So, for this one, the sentence, "I am writing a method called getFormattedReleaseDate that uses a SimpleDateFormat to convert mReleaseDate and return the formatted result" might make it clear that there's nothing else in scope here.

With all problems, defining what the problem/solution relationship is first always helps. Your last step is reaching for code - the solution is never in the compiler!!

Derrick Shepherd
Derrick Shepherd
4,861 Points

That's A great way to look at it Steve! I will use that mode of thinking in the upcoming code challenges. Thanks again