-1

I'm doing a training course at the moment and it's causing me a lot of problems. The code is below. The problem I'm having is that the training course requires a specific solution based on the java.time.LocalDate import.

I've tried multiple different avenues to solve this, but the inbuild compiler keeps blowing up. This is my first attempt at learning Java and it's definitely not helping. Code needs to be added at the 3 points specified.

Feels like failure to have to ask for help on this, but I'm unable to see any solution here.

import java.time.LocalDate;

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
        

        // ====================================
        // 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

        // CODE2: Write code to initialize the date field of the class
        

        // ====================================
        // Do not change the code after this
    }

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

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

        // CODE3: Write code to return the String day of the week of theDate
        

        // ====================================
        // Do not change the code after this
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Tufty
  • 475
  • 8
  • 24
  • 7
    Please post the code rather than a screenshot : if people need to test or debug your code it's much easier to copy/paste it than to transcribe it from a screenshot. – Aaron Jun 23 '20 at 15:32
  • I was unable to get the code to format properly for posting – Tufty Jun 23 '20 at 15:33
  • Your problem is that you try to define a `String[] date`, but `date` is already defined as a `String` (from the method's arguments). Just use a different variable name such as `dateParts` (and rename other references obviously) – Aaron Jun 23 '20 at 15:34
  • 1
    If you want to preserve the formatting, use ``` on line before and after the code. See the [help](https://stackoverflow.com/editing-help#syntax-highlighting). – jiwopene Jun 23 '20 at 15:35
  • If you want, post the code with improper format and I'll reformat it. You'll be able to see how I formatted it by either checking my edit or starting an edit yourself – Aaron Jun 23 '20 at 15:36
  • Have reposted with code now. You'll have to forgive me, but the problems I'm having with this exercise have actually resulted in a migraine and stress attack. Fair to say I don't learn easily. – Tufty Jun 23 '20 at 15:41
  • @Aaron Actually, the code formatter is beng switched and ``` may be better; see https://meta.stackexchange.com/questions/348746/were-switching-to-commonmark?cb=1 – Robert Jun 23 '20 at 15:41
  • @Robert ah thanks, I should definitely have followed jiwopene's link – Aaron Jun 23 '20 at 15:44
  • @Tufty I've improved the formatting, but I now notice that you have provided the base code without your modifications : the `dayOfWeek` method doesn't contain anything else than comments. Did you see my second comment? In the screenshot I had noticed you were trying to define a `date` variable while it was already defined as one of the method's arguments, this likely was what prevented you from compiling. – Aaron Jun 23 '20 at 15:48
  • I decided to blow away my changes. All they were serving to do was confuse, plus I'm fairly certain that at least one change was utter rubbish on my part anyway. – Tufty Jun 23 '20 at 15:49
  • 1
    Alright, then I think you should adapt Arvind's answer by splitting it in two parts in order to respect your template : have the LocalDate.parse(date) assigned to `theDate` in your DateUtil's constructor, and have the `getDayOfWeek` call the `getDayOfWeek` on `theDate`. – Aaron Jun 23 '20 at 15:54
  • So what goes in the 'code 1' section? – Tufty Jun 23 '20 at 15:59
  • To respect the template, you need code1 to instantiate `DateUtils` with the date the method is passed as argument, then call `getDayOfWeek` on that instance and return that result (`return new DateUtils(date).getDayOfWeek()`). As previously said, code2 should parse the constructor's String argument into the `LocalDate theDate` and code3 should retrieve the dayOfTheWeek from that instance field. The template is bothersome, I guess it's made to make you learn the difference between static methods and instance methods, so try to pay attention to that – Aaron Jun 23 '20 at 16:24
  • [Here](https://ideone.com/bIVeuI)'s your template completed (with multiple typos I had in my previous comment fixed) if needed, but try to do it without if possible, copy/pasting won't help you learn. – Aaron Jun 23 '20 at 16:29
  • Thanks Aaron. The course is very badly laid out and yes, it hadn't made clear about static and instance methods in a form I understood, so I'll go and do some more research around those. Once again, thanks for your help and patience. – Tufty Jun 23 '20 at 16:36
  • Does this answer your question? [Return DayOfTheWeek (as an uppercase string) from a given String input](https://stackoverflow.com/questions/59807238/return-dayoftheweek-as-an-uppercase-string-from-a-given-string-input) – Ole V.V. Jun 23 '20 at 16:40

2 Answers2

3

Use LocalDate.parse to parse the date string into a LocalDate instance and then use LocalDate#getDayOfWeek to get day of week from the LocalDate instance.

public static String dayOfWeek(String date) {
    return LocalDate.parse(date).getDayOfWeek().toString();
}

Also, use String#equals to compare the strings i.e. you should write:

System.out.println(expected.equals(theDayOfWeek));

Note that == compares the references, not the content.

[Update]

This is how you can utilize your DateUtil class to do it.

import java.time.LocalDate;

public class ChallengeThree {
    public static String dayOfWeek(String date) {
        return new DateUtil(date).dayOfWeek();
    }

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

class DateUtil {
    LocalDate theDate;

    public DateUtil(String date) {
        theDate = LocalDate.parse(date);
    }

    public String dayOfWeek() {
        return theDate.getDayOfWeek().toString();
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Doesn't work in the context of the exercise. Not a problem with your solution, it's a problem with the exercise. Thank you for the help Arvind. – Tufty Jun 23 '20 at 16:11
  • @Tufty - You are most welcome. I would like to understand what is that which `doesn't work in the context of the exercise` so that I can help you further. – Arvind Kumar Avinash Jun 23 '20 at 16:12
  • Their compiler is very specific in terms of what it allows. The code 1 block needs to specifically reference the DateUtil class and I can't get that to work. // CODE1: Write code to return the day of the week of the String date // using the DateUtil class at the bottom of this file // ==================================== – Tufty Jun 23 '20 at 16:14
  • @Tufty - I've posted an update. I hope it helps. Feel free to comment in case of any further doubt. – Arvind Kumar Avinash Jun 23 '20 at 16:19
  • Thank you for your help on this. Seeing your solution has made things alot clearer. – Tufty Jun 23 '20 at 16:34
0

tl;dr

    LocalDate                          // Represent a date-only, without time-of-day and without time zone.
    .parse( "2000-01-01" )             // Automatically parse a string in standard ISO 8601 format.
    .getDayOfWeek()                    // Returns `DayOfWeek` enum object.
    .equals( DayOfWeek.SATURDAY )      // Comparing `DayOfWeek` objects, returning a `boolean`. 

See this code run live at IdeOne.com.

true

DayOfWeek

The accepted Answer by Avinash is correct, and directly addresses your specific exercise.

But I want to point out that Java offers specific classes to help you with this work.

Specifically, Java offers the DayOfWeek enum class. See Oracle tutorial on enums if you are new to that. You can ask a LocalDate for the enum object representing its day-of-week: LocalDate::getDayOfWeek.

Use smart objects rather than dumb strings. So this code:

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

…should be:

LocalDate ld = LocalDate.parse( "2000-01-01" ) ;  // Standard ISO 8601 format is YYYY-MM-DD. 
DayOfWeek dow = ld.getDayOfWeek() ;
DayOfWeek expectedDow = DayOfWeek.SATURDAY ; 
boolean isExpected = dow.equals( expectedDow ) ;

In real work, I would nest that call to LocalDate.parse in a try-catch to trap for the DateTimeParseException in case of faulty input string.

Or make that a one-liner (not that I recommend doing so).

if( 
    LocalDate                          // Represent a date-only, without time-of-day and without time zone.
    .parse( "2000-01-01" )             // Automatically parse a string in standard ISO 8601 format.
    .getDayOfWeek()                    // Returns `DayOfWeek` enum object.
    .equals( DayOfWeek.SATURDAY )      // Comparing `DayOfWeek` objects, returning a `boolean`. 
) { … }

See this code run live at IdeOne.com.

true

By the way, we can get the name of the day localized.

String output = 
    DayOfWeek.SATURDAY
    .getDisplayName​(
        TextStyle.FULL , 
        Locale.CANADA_FRENCH
    )
;
System.out.println( output ) ;

samedi

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154