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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2000-2016 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. import com.vaadin.data.Result;
  22. /**
  23. * A converter that converts from {@link Date} to {@link String} and back. Uses
  24. * the given locale and {@link DateFormat} for formatting and parsing.
  25. * <p>
  26. * Leading and trailing white spaces are ignored when converting from a String.
  27. * </p>
  28. * <p>
  29. * Override and overwrite {@link #getFormat(Locale)} to use a different format.
  30. * </p>
  31. *
  32. * @author Vaadin Ltd
  33. * @since 8.0
  34. */
  35. public class StringToDateConverter implements Converter<String, Date> {
  36. /**
  37. * Returns the format used by {@link #convertToPresentation(Date, Locale)}
  38. * and {@link #convertToModel(String, 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 format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
  49. DateFormat.MEDIUM, locale);
  50. format.setLenient(false);
  51. return format;
  52. }
  53. @Override
  54. public Result<Date> convertToModel(String value, Locale locale) {
  55. if (value == null) {
  56. return Result.ok(null);
  57. }
  58. // Remove leading and trailing white space
  59. value = value.trim();
  60. ParsePosition parsePosition = new ParsePosition(0);
  61. Date parsedValue = getFormat(locale).parse(value, parsePosition);
  62. if (parsePosition.getIndex() != value.length()) {
  63. return Result.error("Could not convert '" + value);
  64. }
  65. return Result.ok(parsedValue);
  66. }
  67. @Override
  68. public String convertToPresentation(Date value, Locale locale) {
  69. if (value == null) {
  70. return null;
  71. }
  72. return getFormat(locale).format(value);
  73. }
  74. }