Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you use a pattern like </p> <pre><code>DateTimeFormat.forPattern("MMddyyyy") </code></pre> <p>it can read a date like <code>"120100"</code>. What it does is read the <code>12</code> for <code>MM</code>, the <code>01</code> for <code>dd</code>, and <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html" rel="noreferrer"><code>00</code> as a literal value for <code>yyyy</code>, ie. <code>0</code> starting at year <code>0</code>, so <code>0000</code></a>. </p> <p>Because that pattern appears before the <code>MMddyy</code> in your <code>parsers</code> array, it will be used to parse your date string.</p> <p>What you'll want to do, along with reordering or removing that format, is use a pivot year.</p> <pre><code>public static void main(String[] args) throws Exception { String str = "120100"; DateTimeParser[] formats = { DateTimeFormat.forPattern("MM/dd/yyyy").getParser(), DateTimeFormat.forPattern("MM/dd/yy").getParser(), DateTimeFormat.forPattern("MM-dd-yyyy").getParser(), DateTimeFormat.forPattern("MM-dd-yy").getParser(), DateTimeFormat.forPattern("MMddyy").getParser() }; DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, formats).toFormatter() .withPivotYear(1950).withLocale(Locale.US); DateTime dateTime = formatter.parseDateTime(str); System.out.println(dateTime); } </code></pre> <p>prints </p> <pre><code>1900-12-01T00:00:00.000-05:00 </code></pre> <p>I'm still trying to figure out how <code>appendTwoDigitYear</code> works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload