#LocalDate DateTimeParseException

1 messages · Page 1 of 1 (latest)

uneven mural
#

I need help with a little excercise. I am to write a method which returns true or false when a dayOfYear is odd or not respectively. I have to work with a String date ("MAY 22 2013"). I have no other instructions than to print true or false.
So my first thought and intention was to parse the String into a LocalDate date and then use the getDayOfYear() method. This is my code so far:

public static void main (String[]args){
        System.out.println(isOddDate("MAY 22 2013"));
        }

public static boolean isOddDate (String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd yyyy");
        LocalDate localDate = LocalDate.parse(date, formatter);
        if (localDate.getDayOfYear()%2 != 0) {
            return true;
        } else {
            return false;
        }
}

I get the error message Text 'MAY 22 2013' could not be parsed at index 0. I tried the java docs, several other tutorials and I have no idea what exactly I am doing wrong. Can somebody give me a hint please?

glacial walrusBOT
# uneven mural I need help with a little excercise. I am to write a method which returns true o...

Detected code, here are some useful tools:

Formatted code
public static void main(String[] args) {
  System.out.println(isOddDate("MAY 22 2013"));
}
public static boolean isOddDate(String date) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd yyyy");
  LocalDate localDate = LocalDate.parse(date, formatter);
  if (localDate.getDayOfYear() % 2 != 0) {
    return true;
  }
  else {
    return false;
  }
}
#

<@&987246399047479336> please have a look, thanks.

glacial walrusBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

tacit shuttle
#

your problem is the section MAY

#

May might work

#

or if u do have to use MAY, have a look at Month ( enum in java )

uneven mural
#

oh it is case sensitive?

#

I tried it and May actually worked! now i have to somehow change the string to sth i can use....

echo matrix
uneven mural
#

Thanks guys! I think I got a way around!