You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringToDateConverter.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util.converter;
  5. import java.text.DateFormat;
  6. import java.text.ParsePosition;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. /**
  10. * A converter that converts from {@link Date} to {@link String} and back. Uses
  11. * the given locale and {@link DateFormat} for formatting and parsing.
  12. * <p>
  13. * Leading and trailing white spaces are ignored when converting from a String.
  14. * </p>
  15. * <p>
  16. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  17. * </p>
  18. *
  19. * @author Vaadin Ltd
  20. * @version
  21. * @VERSION@
  22. * @since 7.0
  23. */
  24. public class StringToDateConverter implements Converter<String, Date> {
  25. /**
  26. * Returns the format used by {@link #convertToPresentation(Date, Locale)}
  27. * and {@link #convertToModel(String, Locale)}.
  28. *
  29. * @param locale
  30. * The locale to use
  31. * @return A DateFormat instance
  32. */
  33. protected DateFormat getFormat(Locale locale) {
  34. if (locale == null) {
  35. locale = Locale.getDefault();
  36. }
  37. DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
  38. DateFormat.MEDIUM, locale);
  39. f.setLenient(false);
  40. return f;
  41. }
  42. /*
  43. * (non-Javadoc)
  44. *
  45. * @see
  46. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  47. * java.util.Locale)
  48. */
  49. @Override
  50. public Date convertToModel(String value, Locale locale)
  51. throws com.vaadin.data.util.converter.Converter.ConversionException {
  52. if (value == null) {
  53. return null;
  54. }
  55. // Remove leading and trailing white space
  56. value = value.trim();
  57. ParsePosition parsePosition = new ParsePosition(0);
  58. Date parsedValue = getFormat(locale).parse(value, parsePosition);
  59. if (parsePosition.getIndex() != value.length()) {
  60. throw new ConversionException("Could not convert '" + value
  61. + "' to " + getModelType().getName());
  62. }
  63. return parsedValue;
  64. }
  65. /*
  66. * (non-Javadoc)
  67. *
  68. * @see
  69. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  70. * .Object, java.util.Locale)
  71. */
  72. @Override
  73. public String convertToPresentation(Date value, Locale locale)
  74. throws com.vaadin.data.util.converter.Converter.ConversionException {
  75. if (value == null) {
  76. return null;
  77. }
  78. return getFormat(locale).format(value);
  79. }
  80. /*
  81. * (non-Javadoc)
  82. *
  83. * @see com.vaadin.data.util.converter.Converter#getModelType()
  84. */
  85. @Override
  86. public Class<Date> getModelType() {
  87. return Date.class;
  88. }
  89. /*
  90. * (non-Javadoc)
  91. *
  92. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  93. */
  94. @Override
  95. public Class<String> getPresentationType() {
  96. return String.class;
  97. }
  98. }