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

Code showing an error - Date cannot be converted to string by method invocation conversion

Output.html ./Movie.java:10: error: no suitable constructor found for Date(Date) Date dateTime = new Date(mReleaseDate); ^ constructor Date.Date(String) is not applicable (actual argument Date cannot be converted to String by method invocation conversion) constructor Date.Date(int,int,int,int,int,int) is not applicable (actual and formal argument lists differ in length) constructor Date.Date(int,int,int,int,int) is not applicable (actual and formal argument lists differ in length) constructor Date.Date(int,int,int) is not applicable (actual and formal argument lists differ in length) constructor Date.Date(long) is not applicable (actual argument Date cannot be converted to long by method invocation conversion) constructor Date.Date() is not applicable (actual and formal argument lists differ in length) 1 error

Movie.java
import java.util.Date;

public class Movie {

    private String mTitle;
    private Date mReleaseDate;

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

    public String getTitle() {
        return mTitle;
    }

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

    public Date getReleaseDate() {
        return mReleaseDate;
    }

    public void setReleaseDate(Date date) {
        mReleaseDate = date;
    }
}

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jery;

Let's walk through Task 3 as that seems to be where your error is originating.

The instructions state:

The mReleaseDate member variable is already stored as a Date object. Convert it and return it using the SimpleDateFormat variable.

Plan & Perform

If we look at the task directions, we are already given the date variable we need to format in mReleaseDate, which was defined in the prompting code by the code towards the top of the class of

private Date mReleaseDate

and defined in our setter method for release date, setReleaseDate(). We need to convert that to our date format with the formatter we just created in the last task using the convention

formatter.format(mReleaseDate).

Next, we need to get that into a string format since that is what this method returns. We can do that with:

String formattedDate = formatter.format(mReleaseDate);

Then we just need to return our string with return formattedDate. The entire method then would be:

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

Does that make sense? Post back with additional questions.

Happy coding, Ken

Ken Alger
Ken Alger
Treehouse Teacher

Oops, I forgot the closing bracket in my final code snippet. Edited my post and fixed that.

Ken