1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.beanutils.locale.converters;
  17. import java.sql.Timestamp;
  18. import java.text.ParseException;
  19. import java.util.Locale;
  20. /**
  21. * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
  22. * implementation that converts an incoming
  23. * locale-sensitive String into a <code>java.sql.Timestamp</code> object,
  24. * optionally using a default value or throwing a
  25. * {@link org.apache.commons.beanutils.ConversionException}
  26. * if a conversion error occurs.</p>
  27. *
  28. * @author Yauheny Mikulski
  29. */
  30. public class SqlTimestampLocaleConverter extends DateLocaleConverter {
  31. // ----------------------------------------------------------- Constructors
  32. /**
  33. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  34. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  35. * if a conversion error occurs. The locale is the default locale for
  36. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  37. * for the convertion.
  38. *
  39. */
  40. public SqlTimestampLocaleConverter() {
  41. this(false);
  42. }
  43. /**
  44. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  45. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  46. * if a conversion error occurs. The locale is the default locale for
  47. * this instance of the Java Virtual Machine.
  48. *
  49. * @param locPattern Indicate whether the pattern is localized or not
  50. */
  51. public SqlTimestampLocaleConverter(boolean locPattern) {
  52. this(Locale.getDefault(), locPattern);
  53. }
  54. /**
  55. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  56. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  57. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  58. *
  59. * @param locale The locale
  60. */
  61. public SqlTimestampLocaleConverter(Locale locale) {
  62. this(locale, (String) null);
  63. }
  64. /**
  65. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  66. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  67. * if a conversion error occurs.
  68. *
  69. * @param locale The locale
  70. * @param locPattern Indicate whether the pattern is localized or not
  71. */
  72. public SqlTimestampLocaleConverter(Locale locale, boolean locPattern) {
  73. this(locale, (String) null);
  74. }
  75. /**
  76. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  77. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  78. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  79. *
  80. * @param locale The locale
  81. * @param pattern The convertion pattern
  82. */
  83. public SqlTimestampLocaleConverter(Locale locale, String pattern) {
  84. this(locale, pattern, false);
  85. }
  86. /**
  87. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  88. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  89. * if a conversion error occurs.
  90. *
  91. * @param locale The locale
  92. * @param pattern The convertion pattern
  93. * @param locPattern Indicate whether the pattern is localized or not
  94. */
  95. public SqlTimestampLocaleConverter(Locale locale, String pattern, boolean locPattern) {
  96. super(locale, pattern, locPattern);
  97. }
  98. /**
  99. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  100. * that will return the specified default value
  101. * if a conversion error occurs. The locale is the default locale for
  102. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  103. * for the convertion.
  104. *
  105. * @param defaultValue The default value to be returned
  106. */
  107. public SqlTimestampLocaleConverter(Object defaultValue) {
  108. this(defaultValue, false);
  109. }
  110. /**
  111. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  112. * that will return the specified default value
  113. * if a conversion error occurs. The locale is the default locale for
  114. * this instance of the Java Virtual Machine.
  115. *
  116. * @param defaultValue The default value to be returned
  117. * @param locPattern Indicate whether the pattern is localized or not
  118. */
  119. public SqlTimestampLocaleConverter(Object defaultValue, boolean locPattern) {
  120. this(defaultValue, Locale.getDefault(), locPattern);
  121. }
  122. /**
  123. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  124. * that will return the specified default value
  125. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  126. *
  127. * @param defaultValue The default value to be returned
  128. * @param locale The locale
  129. */
  130. public SqlTimestampLocaleConverter(Object defaultValue, Locale locale) {
  131. this(defaultValue, locale, false);
  132. }
  133. /**
  134. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  135. * that will return the specified default value
  136. * if a conversion error occurs.
  137. *
  138. * @param defaultValue The default value to be returned
  139. * @param locale The locale
  140. * @param locPattern Indicate whether the pattern is localized or not
  141. */
  142. public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
  143. this(defaultValue, locale, null, locPattern);
  144. }
  145. /**
  146. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  147. * that will return the specified default value
  148. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  149. *
  150. * @param defaultValue The default value to be returned
  151. * @param locale The locale
  152. * @param pattern The convertion pattern
  153. */
  154. public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, String pattern) {
  155. this(defaultValue, locale, pattern, false);
  156. }
  157. /**
  158. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  159. * that will return the specified default value
  160. * if a conversion error occurs.
  161. *
  162. * @param defaultValue The default value to be returned
  163. * @param locale The locale
  164. * @param pattern The convertion pattern
  165. * @param locPattern Indicate whether the pattern is localized or not
  166. */
  167. public SqlTimestampLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
  168. super(defaultValue, locale, pattern, locPattern);
  169. }
  170. // --------------------------------------------------------- Methods
  171. /**
  172. * Convert the specified locale-sensitive input object into an output object of the
  173. * specified type.
  174. *
  175. * @param value The input object to be converted
  176. * @param pattern The pattern is used for the convertion
  177. *
  178. * @exception ConversionException if conversion cannot be performed
  179. * successfully
  180. */
  181. protected Object parse(Object value, String pattern) throws ParseException {
  182. return new Timestamp(((java.util.Date) super.parse(value, pattern)).getTime());
  183. }
  184. }