yaya30001996
yaya30001996

Reputation: 39

Return DayOfTheWeek (as an uppercase string) from a given String input

This is my first time looking into the Date Api's i don't understand where i'm going wrong. The Question has been commented out so you can see exactly whats expected.

Could someone please simply explain how to solve/approach this problem?

When i get to the DateUtil class>DayofTheWeek method i attempt to return the LocalDate.DayofTheWeek method using the theDate field which by now has been initialised. but it wont work. It keeps saying 'cannot resolve method'?

public class ChallengeThree {
    public static String dayOfWeek(String date) {
        /**
         *** Returns a String storing the day of the week in all capital letters of the
         * given date String
         * Complete the implementation of the DateUtil class and use it in this function
         * Arguments
         * date - a String storing a local date, such as "2000-01-01"
         * Examples
         * dayOfWeek("2000-01-01") returns "SATURDAY"
         */**

        // ====================================
        // Do not change the code before this

        // CODE1: Write code to return the day of the week of the String date
        //        using the DateUtil class at the bottom of this file

        DateUtil newdates= new DateUtil("2000-01-01");
        System.out.println(newdates.dayOfWeek());
        // ====================================
        // Do not change the code after this
    }

//    public static void main(String[] args) {
//        String theDayOfWeek = dayOfWeek("2000-01-01");
        String expected = "SATURDAY";
        // Expected output is
        // true
//        System.out.println(theDayOfWeek == expected);
//    }
}

class DateUtil {
    LocalDate theDate;

    public DateUtil(String date) {
        /**
         * Initialize the theDate field using the String date argument
         * Arguments
         * date - a String storing a local date, such as "2000-01-01"
         */

        // ====================================
        // Do not change the code before this


        LocalDate theNewDate = LocalDate.parse(date);
        this.theDate=theNewDate;
        // ====================================
        // Do not change the code after this
    }

    public String dayOfWeek() {
        /**
         * Return a String the day of the week represented by theDate
         */

        // ====================================
        // Do not chDate theDate = new ange the code before this

         return LocalDate.of(theDate).getDayOfWeek();
        // ====================================
        // Do not change the code after this
    }
}

Upvotes: 2

Views: 1642

Answers (3)

Anonymous
Anonymous

Reputation: 86276

The other answers are fine. I wanted to go closer to what you asked precisely:

It keeps saying 'cannot resolve method'?

As you have seen, the error message comes in this line:

     return LocalDate.of(theDate).getDayOfWeek();

The method it cannot resolve is of(). LocalDate has a couple of overloaded of methods. theDate is already a LocalDate, and as MadProgrammer said in the comment, there is no of method accepting a LocalDate. This is the reason for the error message. BTW, the message I get in my Eclipse says “The method of(int, Month, int) in the type LocalDate is not applicable for the arguments (LocalDate)”.

Since theDate is already a LocalDate, you don’t need that method call at all. Just leave it out and call getDayOfWeek() on theDate directly (I am on purpose leaving to yourself to put it into code; you’re the one supposed to learn from doing this, so you should be the one doing it).

It seems that you are having another problem in that LocalDate.getDayOfWeek() returns a DayOfWeek and your DateUtil.dayOfWeek() is supposed to return a String. You can likely solve it yourself when you get around to it. If not, feel free to follow up in comments.

As a complete aside, for production code I would consider a DateUtil class for this purpose overkill. I understand that this is an assignment, so I have answered it being faithful to the assignment as given.

Upvotes: 0

Mehrdad Yami
Mehrdad Yami

Reputation: 1681

Edited

I made a small change in your code (it worked)

DateUtil

class DateUtil {

private LocalDate date;

public DateUtil(LocalDate date) {
    this.date = date;
}

public String dayOfWeek() {
    return String.valueOf(date.getDayOfWeek());

    }
}

ChallengeThree

public class ChallengeThree {

public static void main(String[] args) {

    String theDayOfWeek = dayOfWeek("2000-01-01");
    System.out.println(theDayOfWeek);

}

public static String dayOfWeek(String date) {

    LocalDate localDate = LocalDate.parse(date);
    DateUtil dateUtil = new DateUtil(localDate);
    return dateUtil.dayOfWeek();

  }

}

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 338624

You are making this much too complicated.

One problem is that you are thinking in text, using dumb strings rather than smart objects. Do not be passing around the string "2000-01-01", pass around a LocalDate object. Do not pass around the string SATURDAY, pass around the DayOfWeek.SATURDAY object.

LocalDate
.parse(
    "2000-01-01"
)
.getDayOfWeek()
.equals(
    DayOfWeek.SATURDAY 
)

If you insist on using strings against my advice, you can get the name of the DayOfWeek enum object as text by calling toString.

String output = DayOfWeek.SATURDAY.toString() ;

Going the other direction, calling DayOfWeek.valueOf.

DayOfWeek dow = DayOfWeek.valueOf( "SATURDAY" ) ;

Upvotes: 1

Related Questions