title: Parsing date and AM/PM using java 8 DateTime API
body: <p>I'm trying to parse a date time string using java 8 DateTime API.</p>
  
  <p>The string to parse contains date, month, year and AM/PM marker such as <code>17/02/2015 PM</code>.
  I use the following pattern: <code>dd/MM/yyyy aa</code> and I expect the parsed LocalDateTime time part to be set at <code>12:00:00</code> or <code>00:00:00</code> depending on the AM/PM marker.</p>
  
  <p>Using the former <code>java.text.SimpleDateFormat</code> API, the parsing works as expected using this pattern.</p>
  
  <p>But when i use java 8 DateTime API and run the following code:</p>
  
  <pre><code>LocalDateTime.parse("17/02/2015 PM", DateTimeFormatter.ofPattern("dd/MM/yyyy aa"));
  </code></pre>
  
  <p>I got the following exception: </p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Too many pattern letters: a
  at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1765)
  at java.time.format.DateTimeFormatterBuilder.parsePattern(DateTimeFormatterBuilder.java:1604)
  at java.time.format.DateTimeFormatterBuilder.appendPattern(DateTimeFormatterBuilder.java:1572)
  at java.time.format.DateTimeFormatter.ofPattern(DateTimeFormatter.java:534)
  </code></pre>
  
  <p>If i switch the pattern to <code>dd/MM/yyyy a</code> then i got the folowing exception:</p>
  
  <pre><code>Exception in thread "main" java.time.format.DateTimeParseException: Text '17/02/2015 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
      at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
      at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
      at java.time.LocalDateTime.parse(LocalDateTime.java:492)
  Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
      at java.time.LocalDateTime.from(LocalDateTime.java:461)
      at java.time.LocalDateTime$$Lambda$7/474675244.queryFrom(Unknown Source)
      at java.time.format.Parsed.query(Parsed.java:226)
      at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
      ... 2 more
  Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {AmPmOfDay=1},ISO resolved to 2015-02-17 of type java.time.format.Parsed
      at java.time.LocalTime.from(LocalTime.java:409)
      at java.time.LocalDateTime.from(LocalDateTime.java:457)
      ... 5 more
  </code></pre>
  
  <p>It's also weird that when i do the reverse operation, format, it works fine:</p>
  
  <pre><code> System.out.println(LocalDateTime.of(2015, 02, 17, 0, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
   System.out.println(LocalDateTime.of(2015, 02, 17, 12, 0, 0).format(DateTimeFormatter.ofPattern("dd/MM/yyyy a")));
  </code></pre>
  
  <p>respectively prints <code>17/02/2015 AM</code> and <code>17/02/2015 PM</code>.</p>
  
  <p>The javadoc for <code>java.time.DateTimeFormatter</code> says (§Resolving, step #6):</p>
  
  <blockquote>
    <ol start="6">
    <li>A LocalTime is formed if there is at least an hour-of-day available. This involves providing default values for minute, second and fraction of second. </li>
    </ol>
  </blockquote>
  
  <p>What i understand is that the field hour-of-day is mandatory to parse a LocalDateTime... Isn't there any way to parse the time part only using the AM/PM field only?</p>