4
public class Solution {

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

    public static boolean isDateOdd(String date) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd yyyy");
        formatter = formatter.withLocale(Locale.ENGLISH); 
        LocalDate outputDate = LocalDate.parse(date, formatter);
        return ((outputDate.getDayOfYear()%2!=0)?true:false);
    }
}

I want to know, if number of days, that passed from beginning of the year to some date is odd. I try to use LocalDate to parse date from my string (MAY 1 2013), but I get error:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'MAY 1 2013' could not be parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) at java.time.LocalDate.parse(LocalDate.java:400) at com.javarush.task.task08.task0827.Solution.isDateOdd(Solution.java:23) at com.javarush.task.task08.task0827.Solution.main(Solution.java:16)

Where's a problem?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Aldres
  • 185
  • 1
  • 3
  • 16

3 Answers3

7

If you want to use the month's input with all the capital letters, ex.MAY, you have to use the case-insensitive DateTimeFormatter:

public static boolean isDateOdd(String date) {
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern("MMM d yyyy")
            .toFormatter(Locale.ENGLISH);
    LocalDate outputDate = LocalDate.parse(date, formatter);
    return (outputDate.getDayOfYear() % 2 != 0);
}

As the documentation of the parseCaseSensitive() method says:

Since the default is case sensitive, this method should only be used after a previous call to #parseCaseInsensitive.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Yes! Problem was in parseCaseInsensitive. Thank you. So, without .parseCaseIntensitive, it will work only with "May 01 1998", won't it? – Aldres Sep 29 '17 at 10:33
  • 2
    Case insensitivity means the processing is not influenced with the leter casing. So all of the case-variations of the month will work: `mAy`, `MAY`, `may`, `mAY` etc... – Nikolas Charalambidis Sep 29 '17 at 10:36
  • @Aldres, you are correct, with the default case-sensitive parsing only May (capital M, small ay) will be recognized. – Ole V.V. Oct 01 '17 at 08:11
2

Amend MAY to May, and 1 to 01 and it will work.

achAmháin
  • 4,176
  • 4
  • 17
  • 40
2

Your day part should have two digits, i.e "MAY 01 2013" .

Then if you really want to pass upper-case month names, you should use a builder along with parseCaseInsensitive() .

Putting it all together :

public static boolean isDateOdd(String date) {

    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    builder.parseCaseInsensitive();
    builder.appendPattern("MMM dd yyyy");
    DateTimeFormatter formatter = builder.toFormatter(Locale.ENGLISH); 

    LocalDate outputDate = LocalDate.parse(date, formatter);
    return ((outputDate.getDayOfYear()%2!=0)?true:false);
 }
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44