@Test public void shouldUseTheDefaultLocale() throws ParseException { when(request.getAttribute(LOCALE_KEY + ".request")).thenReturn(null); when(request.getSession()).thenReturn(session); when(session.getAttribute(LOCALE_KEY + ".session")). thenReturn(null); when(context.getAttribute(LOCALE_KEY + ".application")). thenReturn(null); when(context.getInitParameter(LOCALE_KEY)). thenReturn(null); when(request.getLocale()).thenReturn(null); Date date = new SimpleDateFormat("dd/MM/yyyy").parse("10/05/2010"); String formattedToday = DateFormat.getDateInstance(DateFormat.SHORT).format(date); assertThat(converter.convert(formattedToday, Date.class, bundle), is(equalTo(date))); }
public Date convert(String value, Class<? extends Date> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatDateTime = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); try { return formatDateTime.parse(value); } catch (ParseException pe) { DateFormat formatDate = DateFormat.getDateInstance( DateFormat.SHORT, locale); try { return formatDate.parse(value); } catch (ParseException pe1) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_date"), value)); } } }
LocaleBasedDateConverter implements Converter<Date> { public Date convert(String value, Class<? extends Date> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatDateTime = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); try { return formatDateTime.parse(value); } catch (ParseException pe) { DateFormat formatDate = DateFormat.getDateInstance( DateFormat.SHORT, locale); try { return formatDate.parse(value); } catch (ParseException pe1) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_date"), value)); } } } }
LocaleBasedDateConverter implements Converter<Date> { public Date convert(String value, Class<? extends Date> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatDateTime = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); try { return formatDateTime.parse(value); } catch (ParseException pe) { DateFormat formatDate = DateFormat.getDateInstance( DateFormat.SHORT, locale); try { return formatDate.parse(value); } catch (ParseException pe1) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_date"), value)); } } } LocaleBasedDateConverter(Localization localization); }
LocaleBasedDateConverter implements Converter<Date> { public Date convert(String value, Class<? extends Date> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatDateTime = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); try { return formatDateTime.parse(value); } catch (ParseException pe) { DateFormat formatDate = DateFormat.getDateInstance( DateFormat.SHORT, locale); try { return formatDate.parse(value); } catch (ParseException pe1) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_date"), value)); } } } LocaleBasedDateConverter(Localization localization); Date convert(String value, Class<? extends Date> type, ResourceBundle bundle); }
LocaleBasedDateConverter implements Converter<Date> { public Date convert(String value, Class<? extends Date> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat formatDateTime = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, locale); try { return formatDateTime.parse(value); } catch (ParseException pe) { DateFormat formatDate = DateFormat.getDateInstance( DateFormat.SHORT, locale); try { return formatDate.parse(value); } catch (ParseException pe1) { throw new ConversionError(MessageFormat.format( bundle.getString("is_not_a_valid_date"), value)); } } } LocaleBasedDateConverter(Localization localization); Date convert(String value, Class<? extends Date> type, ResourceBundle bundle);  }