title: LocalDate parse throws exceptions‽
body: <p>I am doing the following programming exercise: <a href="https://www.codewars.com/kata/56eb0be52caf798c630013c0" rel="nofollow noreferrer">Unlucky Days</a>. The statement is:</p>
  
  <blockquote>
    <p>Friday 13th or Black Friday is considered as unlucky day. Calculate
    how many unlucky days are in the given year.</p>
    
    <p>Find the number of Friday 13th in the given year.</p>
    
    <p>Input: Year as an integer.</p>
    
    <p>Output: Number of Black Fridays in the year as an integer.</p>
    
    <p>Examples:</p>
    
    <p>unluckyDays(2015) == 3 unluckyDays(1986) == 1</p>
  </blockquote>
  
  <p>First I tried the following code:</p>
  
  <pre><code>import java.time.*;
  import java.time.format.*;
  import java.util.Locale;
  
  public class Kata {
    public static int unluckyDays/**/(int year) {
      System.out.println("\nyear: "+year);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/dd");
      for(int i = 1; i &lt;= 12; i++){
        LocalDate date = LocalDate.parse(String.format("%d/%d/13",year,i));
        DayOfWeek dow = date.getDayOfWeek();
        String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
        System.out.println("\noutput: "+output);
      }
      return 0;
    }
  }
  </code></pre>
  
  <p>Where we get the following exception:</p>
  
  <pre><code>java.time.format.DateTimeParseException: Text '2015/1/13' could not be parsed at index 4
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
      at java.base/java.time.LocalDate.parse(LocalDate.java:428)
      at java.base/java.time.LocalDate.parse(LocalDate.java:413)
      at Kata.unluckyDays(Kata.java:10)
  </code></pre>
  
  <p>So as we see, the '/' at index 4 in the string used for <code>LocalDate.parse(String.format("%d/%d/13",year,i));</code> is wrong.</p>
  
  <p>Then I decided to change the format to use '-' instead of '/'</p>
  
  <pre><code>import java.time.*;
  import java.time.format.*;
  import java.util.Locale;
  
  public class Kata {
    public static int unluckyDays/**/(int year) {
      System.out.println("\nyear: "+year);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-dd");
      for(int i = 1; i &lt;= 12; i++){
        LocalDate date = LocalDate.parse(String.format("%d-%d-13",year,i));
        DayOfWeek dow = date.getDayOfWeek();
        String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
        System.out.println("\noutput: "+output);
      }
      return 0;
    }
  }
  </code></pre>
  
  <p>And with the previous code, the exception being thrown is:</p>
  
  <pre><code>java.time.format.DateTimeParseException: Text '2015-1-13' could not be parsed at index 5
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
      at java.base/java.time.LocalDate.parse(LocalDate.java:428)
      at java.base/java.time.LocalDate.parse(LocalDate.java:413)
      at Kata.unluckyDays(Kata.java:10)
  </code></pre>
  
  <p>So as we see there is a difficulty with the one digit month '1'.</p>
  
  <p>To continue I put the condition to pad with a 0 those months which only have one digit:</p>
  
  <pre><code>import java.time.*;
  import java.time.format.*;
  import java.util.Locale;
  
  public class Kata {
    public static int unluckyDays/**/(int year) {
      System.out.println("\nyear: "+year);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-dd");
      int count = 0;
      for(int i = 1; i &lt;= 12; i++){
        LocalDate date = LocalDate.parse(String.format("%d-%s-13",year,String.valueOf(i).length() &lt; 2 ? "0"+i : i));
        DayOfWeek dow = date.getDayOfWeek();
        String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
        System.out.println("\noutput: "+output);
        if(output.equals("Friday")){
          count++;  
        }
      }
      return count;
    }
  }
  </code></pre>
  
  <p>With this condition it works. However how could we indicate in the <code>DateTimeFormatter.ofPattern("yyyy-M-dd");</code> that months can have only one digit.</p>
  
  <p>In addition, what should we do to use a different date format, for example: <code>DateTimeFormatter.ofPattern("dd-M-yyyy");</code></p>
  
  <p>Because of I have tried:</p>
  
  <pre><code>import java.time.*;
  import java.time.format.*;
  import java.util.Locale;
  
  public class Kata {
    public static int unluckyDays/**/(int year) {
      System.out.println("\nyear: "+year);
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-M-yyyy");
      int count = 0;
      for(int i = 1; i &lt;= 12; i++){
        LocalDate date = LocalDate.parse(String.format("13-%s-%d",String.valueOf(i).length() &lt; 2 ? "0"+i : i,year));
        DayOfWeek dow = date.getDayOfWeek();
        String output = dow.getDisplayName(TextStyle.FULL, Locale.US);
        System.out.println("\noutput: "+output);
        if(output.equals("Friday")){
          count++;  
        }
      }
      return count;
    }
  }
  </code></pre>
  
  <p>Being the output quite interesting:</p>
  
  <pre><code>java.time.format.DateTimeParseException: Text '13-01-2015' could not be parsed at index 0
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
      at java.base/java.time.LocalDate.parse(LocalDate.java:428)
      at java.base/java.time.LocalDate.parse(LocalDate.java:413)
      at Kata.unluckyDays(Kata.java:11)
  </code></pre>
  
  <p>As we observe the '13' for days is causing an exception, at index 0, which I do not understand.</p>
  
  <p>In addition I have read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date">How to determine day of week by passing specific date?</a></li>
  <li><a href="https://stackoverflow.com/questions/546505/calculating-future-occurrences-of-friday-the-13th">Calculating future occurrences of Friday the 13th</a></li>
  <li><a href="https://www.geeksforgeeks.org/localdate-parse-method-in-java-with-examples/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/localdate-parse-method-in-java-with-examples/</a></li>
  </ul>
  
  <p>How could we know, why LocalDate parse throws exceptions‽</p>