1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.beanutils.locale.converters; 19 20 import java.sql.Timestamp; 21 import java.text.ParseException; 22 import java.util.Locale; 23 24 /** 25 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 26 * implementation that converts an incoming 27 * locale-sensitive String into a <code>java.sql.Timestamp</code> object, 28 * optionally using a default value or throwing a 29 * {@link org.apache.commons.beanutils.ConversionException} 30 * if a conversion error occurs.</p> 31 * 32 * @version $Id$ 33 */ 34 35 public class SqlTimestampLocaleConverter extends DateLocaleConverter { 36 37 38 // ----------------------------------------------------------- Constructors 39 40 /** 41 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 42 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 43 * if a conversion error occurs. The locale is the default locale for 44 * this instance of the Java Virtual Machine and an unlocalized pattern is used 45 * for the convertion. 46 * 47 */ 48 public SqlTimestampLocaleConverter() { 49 50 this(false); 51 } 52 53 /** 54 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 55 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 56 * if a conversion error occurs. The locale is the default locale for 57 * this instance of the Java Virtual Machine. 58 * 59 * @param locPattern Indicate whether the pattern is localized or not 60 */ 61 public SqlTimestampLocaleConverter(final boolean locPattern) { 62 63 this(Locale.getDefault(), locPattern); 64 } 65 66 /** 67 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 68 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 69 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 70 * 71 * @param locale The locale 72 */ 73 public SqlTimestampLocaleConverter(final Locale locale) { 74 75 this(locale, (String) null); 76 } 77 78 /** 79 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 80 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 81 * if a conversion error occurs. 82 * 83 * @param locale The locale 84 * @param locPattern Indicate whether the pattern is localized or not 85 */ 86 public SqlTimestampLocaleConverter(final Locale locale, final boolean locPattern) { 87 88 this(locale, (String) null); 89 } 90 91 /** 92 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 93 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 94 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 95 * 96 * @param locale The locale 97 * @param pattern The convertion pattern 98 */ 99 public SqlTimestampLocaleConverter(final Locale locale, final String pattern) { 100 101 this(locale, pattern, false); 102 } 103 104 /** 105 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 106 * that will throw a {@link org.apache.commons.beanutils.ConversionException} 107 * if a conversion error occurs. 108 * 109 * @param locale The locale 110 * @param pattern The convertion pattern 111 * @param locPattern Indicate whether the pattern is localized or not 112 */ 113 public SqlTimestampLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) { 114 115 super(locale, pattern, locPattern); 116 } 117 118 /** 119 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 120 * that will return the specified default value 121 * if a conversion error occurs. The locale is the default locale for 122 * this instance of the Java Virtual Machine and an unlocalized pattern is used 123 * for the convertion. 124 * 125 * @param defaultValue The default value to be returned 126 */ 127 public SqlTimestampLocaleConverter(final Object defaultValue) { 128 this(defaultValue, false); 129 } 130 131 /** 132 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 133 * that will return the specified default value 134 * if a conversion error occurs. The locale is the default locale for 135 * this instance of the Java Virtual Machine. 136 * 137 * @param defaultValue The default value to be returned 138 * @param locPattern Indicate whether the pattern is localized or not 139 */ 140 public SqlTimestampLocaleConverter(final Object defaultValue, final boolean locPattern) { 141 142 this(defaultValue, Locale.getDefault(), locPattern); 143 } 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 */ 153 public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale) { 154 155 this(defaultValue, locale, false); 156 } 157 158 /** 159 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 160 * that will return the specified default value 161 * if a conversion error occurs. 162 * 163 * @param defaultValue The default value to be returned 164 * @param locale The locale 165 * @param locPattern Indicate whether the pattern is localized or not 166 */ 167 public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) { 168 169 this(defaultValue, locale, null, locPattern); 170 } 171 172 /** 173 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 174 * that will return the specified default value 175 * if a conversion error occurs. An unlocalized pattern is used for the convertion. 176 * 177 * @param defaultValue The default value to be returned 178 * @param locale The locale 179 * @param pattern The convertion pattern 180 */ 181 public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) { 182 183 this(defaultValue, locale, pattern, false); 184 } 185 186 /** 187 * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 188 * that will return the specified default value 189 * if a conversion error occurs. 190 * 191 * @param defaultValue The default value to be returned 192 * @param locale The locale 193 * @param pattern The convertion pattern 194 * @param locPattern Indicate whether the pattern is localized or not 195 */ 196 public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) { 197 198 super(defaultValue, locale, pattern, locPattern); 199 } 200 201 // --------------------------------------------------------- Methods 202 203 /** 204 * Convert the specified locale-sensitive input object into an output object of the 205 * specified type. 206 * 207 * @param value The input object to be converted 208 * @param pattern The pattern is used for the convertion 209 * @return The converted value 210 * 211 * @throws org.apache.commons.beanutils.ConversionException if conversion 212 * cannot be performed successfully 213 * @throws ParseException if an error occurs parsing a String to a Number 214 */ 215 @Override 216 protected Object parse(final Object value, final String pattern) throws ParseException { 217 218 return new Timestamp(((java.util.Date) super.parse(value, pattern)).getTime()); 219 } 220 }