Note that there are some explanatory texts on larger screens.

plurals
  1. POJoda Time & parsing two digit year correctly based on pivot
    primarykey
    data
    text
    <p>I have the following (somewhat abbreviated) code to parse <code>String</code> input into a Joda Time <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html" rel="nofollow"><code>DateTime</code></a> object. I need to properly handle multiple formats, including four &amp; two digit years. </p> <pre><code>setupValidDateFormats(); DateTime date = convertToDateTime(thisField.getText()); if (date != null) { thisField.setText(date.toString("MM/dd/yyyy")); } else { System.out.println("Invalid date"); } private void setupValidDateFormats() { 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("MMddyyyy").getParser(), DateTimeFormat.forPattern("MMddyy").getParser() }; formatter = new DateTimeFormatterBuilder().append(null, formats).appendTwoDigitYear(1950, true).toFormatter() .withLocale(Locale.US); } private DateTime convertToDateTime(String input) { if (isNullOrEmpty(input)) { return null; } DateTime date; try { // When you parse a date, it'll throw an exception for not only invalid formats, // but for invalid values such as 09/31/2013 or 02/30/2013. Leap year is included as well. date = formatter.parseDateTime(input); } catch (Exception e) { // User input a date in the incorrect format, or an invalid value. // Example: 02/30/2013 is invalid. date = null; } return date; } </code></pre> <p>The issue I'm having is that when the user enters <code>120100</code>, I expect it to be able to parse correctly and ultimately output at 12/01/1900. However, <code>formatter.parseDateTime(input);</code> in <code>convertToDateTime</code> throws an <code>IllegalArgumentException</code> instead, and the method returns <code>null</code>.</p> <p>Probably worth noting that if I remove the <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html#appendTwoDigitYear%28int%29" rel="nofollow"><code>appendTwoDigitYear</code></a> from the <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html" rel="nofollow"><code>DateTimeFormatterBuilder</code></a> chain, the input does parse successfully, but <code>120100</code> becomes <code>12/01/0000</code>, but that's not what I need.</p> <p>Am I misunderstanding the Joda Time API? Shouldn't <a href="http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html#appendTwoDigitYear%28int%29" rel="nofollow"><code>appendTwoDigitYear</code></a> with the 1950 pivot handle the 00 two year digit as 1900?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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