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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.util.converter;
  17. import java.text.DateFormat;
  18. import java.text.ParsePosition;
  19. import java.util.Date;
  20. import java.util.Locale;
  21. /**
  22. * A converter that converts from {@link Date} to {@link String} and back. Uses
  23. * the given locale and {@link DateFormat} for formatting and parsing.
  24. * <p>
  25. * Leading and trailing white spaces are ignored when converting from a String.
  26. * </p>
  27. * <p>
  28. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  29. * </p>
  30. *
  31. * @author Vaadin Ltd
  32. * @since 7.0
  33. */
  34. public class StringToDateConverter implements Converter<String, Date> {
  35. /**
  36. * Returns the format used by
  37. * {@link #convertToPresentation(Date, Class,Locale)} and
  38. * {@link #convertToModel(String, Class, Locale)}.
  39. *
  40. * @param locale
  41. * The locale to use
  42. * @return A DateFormat instance
  43. */
  44. protected DateFormat getFormat(Locale locale) {
  45. if (locale == null) {
  46. locale = Locale.getDefault();
  47. }
  48. DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
  49. DateFormat.MEDIUM, locale);
  50. f.setLenient(false);
  51. return f;
  52. }
  53. /*
  54. * (non-Javadoc)
  55. *
  56. * @see
  57. * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
  58. * java.lang.Class, java.util.Locale)
  59. */
  60. @Override
  61. public Date convertToModel(String value, Class<? extends Date> targetType,
  62. Locale locale)
  63. throws com.vaadin.data.util.converter.Converter.ConversionException {
  64. if (targetType != getModelType()) {
  65. throw new ConversionException("Converter only supports "
  66. + getModelType().getName() + " (targetType was "
  67. + targetType.getName() + ")");
  68. }
  69. if (value == null) {
  70. return null;
  71. }
  72. // Remove leading and trailing white space
  73. value = value.trim();
  74. ParsePosition parsePosition = new ParsePosition(0);
  75. Date parsedValue = getFormat(locale).parse(value, parsePosition);
  76. if (parsePosition.getIndex() != value.length()) {
  77. throw new ConversionException("Could not convert '" + value
  78. + "' to " + getModelType().getName());
  79. }
  80. return parsedValue;
  81. }
  82. /*
  83. * (non-Javadoc)
  84. *
  85. * @see
  86. * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
  87. * .Object, java.lang.Class, java.util.Locale)
  88. */
  89. @Override
  90. public String convertToPresentation(Date value,
  91. Class<? extends String> targetType, Locale locale)
  92. throws com.vaadin.data.util.converter.Converter.ConversionException {
  93. if (value == null) {
  94. return null;
  95. }
  96. return getFormat(locale).format(value);
  97. }
  98. /*
  99. * (non-Javadoc)
  100. *
  101. * @see com.vaadin.data.util.converter.Converter#getModelType()
  102. */
  103. @Override
  104. public Class<Date> getModelType() {
  105. return Date.class;
  106. }
  107. /*
  108. * (non-Javadoc)
  109. *
  110. * @see com.vaadin.data.util.converter.Converter#getPresentationType()
  111. */
  112. @Override
  113. public Class<String> getPresentationType() {
  114. return String.class;
  115. }
  116. }