@Test public void convertToObjetErrors() { final JodaDateConverter conv = new JodaDateConverter(); try { conv.convertToObject("31.11.09", Locale.GERMAN); fail("ConversionException exprected."); } catch (final ConversionException ex) { } try { conv.convertToObject("31.11.", Locale.GERMAN); fail("ConversionException exprected."); } catch (final ConversionException ex) { } try { conv.convertToObject("11/31/1970", Locale.ENGLISH); fail("ConversionException exprected."); } catch (final ConversionException ex) { } try { conv.convertToObject("11/31/70", Locale.ENGLISH); fail("ConversionException exprected."); } catch (final ConversionException ex) { } try { conv.convertToObject("11/31", Locale.ENGLISH); fail("ConversionException exprected."); } catch (final ConversionException ex) { } }
@Override public DateMidnight convertToObject(final String value, final Locale locale) { if (StringUtils.isBlank(value) == true) { return null; } final String[] formatStrings = getFormatStrings(locale); final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length]; for (int i = 0; i < formatStrings.length; i++) { dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale); } DateMidnight date = null; for (final DateTimeFormatter formatter : dateFormats) { try { date = DateMidnight.parse(value, formatter); break; } catch (final Exception ex) { if (log.isDebugEnabled() == true) { log.debug(ex.getMessage(), ex); } } } if (date != null) { return date; } else { log.info("Unparseable date string (user's input): " + value + " for locale " + locale); throw new ConversionException("validation.error.general"); } }
JodaDateConverter implements IConverter<DateMidnight> { @Override public DateMidnight convertToObject(final String value, final Locale locale) { if (StringUtils.isBlank(value) == true) { return null; } final String[] formatStrings = getFormatStrings(locale); final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length]; for (int i = 0; i < formatStrings.length; i++) { dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale); } DateMidnight date = null; for (final DateTimeFormatter formatter : dateFormats) { try { date = DateMidnight.parse(value, formatter); break; } catch (final Exception ex) { if (log.isDebugEnabled() == true) { log.debug(ex.getMessage(), ex); } } } if (date != null) { return date; } else { log.info("Unparseable date string (user's input): " + value + " for locale " + locale); throw new ConversionException("validation.error.general"); } } }
JodaDateConverter implements IConverter<DateMidnight> { @Override public DateMidnight convertToObject(final String value, final Locale locale) { if (StringUtils.isBlank(value) == true) { return null; } final String[] formatStrings = getFormatStrings(locale); final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length]; for (int i = 0; i < formatStrings.length; i++) { dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale); } DateMidnight date = null; for (final DateTimeFormatter formatter : dateFormats) { try { date = DateMidnight.parse(value, formatter); break; } catch (final Exception ex) { if (log.isDebugEnabled() == true) { log.debug(ex.getMessage(), ex); } } } if (date != null) { return date; } else { log.info("Unparseable date string (user's input): " + value + " for locale " + locale); throw new ConversionException("validation.error.general"); } } JodaDateConverter(); }
JodaDateConverter implements IConverter<DateMidnight> { @Override public DateMidnight convertToObject(final String value, final Locale locale) { if (StringUtils.isBlank(value) == true) { return null; } final String[] formatStrings = getFormatStrings(locale); final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length]; for (int i = 0; i < formatStrings.length; i++) { dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale); } DateMidnight date = null; for (final DateTimeFormatter formatter : dateFormats) { try { date = DateMidnight.parse(value, formatter); break; } catch (final Exception ex) { if (log.isDebugEnabled() == true) { log.debug(ex.getMessage(), ex); } } } if (date != null) { return date; } else { log.info("Unparseable date string (user's input): " + value + " for locale " + locale); throw new ConversionException("validation.error.general"); } } JodaDateConverter(); String getPattern(); @Override String convertToString(final DateMidnight value, final Locale locale); @Override DateMidnight convertToObject(final String value, final Locale locale); }
JodaDateConverter implements IConverter<DateMidnight> { @Override public DateMidnight convertToObject(final String value, final Locale locale) { if (StringUtils.isBlank(value) == true) { return null; } final String[] formatStrings = getFormatStrings(locale); final DateTimeFormatter[] dateFormats = new DateTimeFormatter[formatStrings.length]; for (int i = 0; i < formatStrings.length; i++) { dateFormats[i] = getDateTimeFormatter(formatStrings[i], locale); } DateMidnight date = null; for (final DateTimeFormatter formatter : dateFormats) { try { date = DateMidnight.parse(value, formatter); break; } catch (final Exception ex) { if (log.isDebugEnabled() == true) { log.debug(ex.getMessage(), ex); } } } if (date != null) { return date; } else { log.info("Unparseable date string (user's input): " + value + " for locale " + locale); throw new ConversionException("validation.error.general"); } } JodaDateConverter(); String getPattern(); @Override String convertToString(final DateMidnight value, final Locale locale); @Override DateMidnight convertToObject(final String value, final Locale locale); static final int PIVOT_YEAR; }