@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"); Calendar cal = new GregorianCalendar(); cal.setTime(date); String formattedToday = DateFormat.getDateInstance(DateFormat.SHORT).format(date); assertThat(converter.convert(formattedToday, Calendar.class, bundle), is(equalTo(cal))); }
public Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale); try { Date date = format.parse(value); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } catch (ParseException e) { throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_date"), value)); } }
LocaleBasedCalendarConverter implements Converter<Calendar> { public Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale); try { Date date = format.parse(value); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } catch (ParseException e) { throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_date"), value)); } } }
LocaleBasedCalendarConverter implements Converter<Calendar> { public Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale); try { Date date = format.parse(value); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } catch (ParseException e) { throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_date"), value)); } } LocaleBasedCalendarConverter(Localization localization); }
LocaleBasedCalendarConverter implements Converter<Calendar> { public Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale); try { Date date = format.parse(value); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } catch (ParseException e) { throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_date"), value)); } } LocaleBasedCalendarConverter(Localization localization); Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle); }
LocaleBasedCalendarConverter implements Converter<Calendar> { public Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } Locale locale = localization.getLocale(); if (locale == null) { locale = Locale.getDefault(); } DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale); try { Date date = format.parse(value); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); return calendar; } catch (ParseException e) { throw new ConversionError(MessageFormat.format(bundle.getString("is_not_a_valid_date"), value)); } } LocaleBasedCalendarConverter(Localization localization); Calendar convert(String value, Class<? extends Calendar> type, ResourceBundle bundle);  }