001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.beanutils.locale.converters;
019
020import java.sql.Timestamp;
021import java.text.ParseException;
022import java.util.Locale;
023
024/**
025 * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
026 * implementation that converts an incoming
027 * locale-sensitive String into a <code>java.sql.Timestamp</code> object,
028 * optionally using a default value or throwing a
029 * {@link org.apache.commons.beanutils.ConversionException}
030 * if a conversion error occurs.</p>
031 *
032 */
033
034public class SqlTimestampLocaleConverter extends DateLocaleConverter {
035
036    /**
037     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
038     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
039     * if a conversion error occurs. The locale is the default locale for
040     * this instance of the Java Virtual Machine and an unlocalized pattern is used
041     * for the convertion.
042     *
043     */
044    public SqlTimestampLocaleConverter() {
045
046        this(false);
047    }
048
049    /**
050     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
051     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
052     * if a conversion error occurs. The locale is the default locale for
053     * this instance of the Java Virtual Machine.
054     *
055     * @param locPattern    Indicate whether the pattern is localized or not
056     */
057    public SqlTimestampLocaleConverter(final boolean locPattern) {
058
059        this(Locale.getDefault(), locPattern);
060    }
061
062    /**
063     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
064     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
065     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
066     *
067     * @param locale        The locale
068     */
069    public SqlTimestampLocaleConverter(final Locale locale) {
070
071        this(locale, (String) null);
072    }
073
074    /**
075     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
076     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
077     * if a conversion error occurs.
078     *
079     * @param locale        The locale
080     * @param locPattern    Indicate whether the pattern is localized or not
081     */
082    public SqlTimestampLocaleConverter(final Locale locale, final boolean locPattern) {
083
084        this(locale, (String) null);
085    }
086
087    /**
088     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
089     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
090     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
091     *
092     * @param locale        The locale
093     * @param pattern       The convertion pattern
094     */
095    public SqlTimestampLocaleConverter(final Locale locale, final String pattern) {
096
097        this(locale, pattern, false);
098    }
099
100    /**
101     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
102     * that will throw a {@link org.apache.commons.beanutils.ConversionException}
103     * if a conversion error occurs.
104     *
105     * @param locale        The locale
106     * @param pattern       The convertion pattern
107     * @param locPattern    Indicate whether the pattern is localized or not
108     */
109    public SqlTimestampLocaleConverter(final Locale locale, final String pattern, final boolean locPattern) {
110
111        super(locale, pattern, locPattern);
112    }
113
114    /**
115     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
116     * that will return the specified default value
117     * if a conversion error occurs. The locale is the default locale for
118     * this instance of the Java Virtual Machine and an unlocalized pattern is used
119     * for the convertion.
120     *
121     * @param defaultValue  The default value to be returned
122     */
123    public SqlTimestampLocaleConverter(final Object defaultValue) {
124        this(defaultValue, false);
125    }
126
127    /**
128     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
129     * that will return the specified default value
130     * if a conversion error occurs. The locale is the default locale for
131     * this instance of the Java Virtual Machine.
132     *
133     * @param defaultValue  The default value to be returned
134     * @param locPattern    Indicate whether the pattern is localized or not
135     */
136    public SqlTimestampLocaleConverter(final Object defaultValue, final boolean locPattern) {
137
138        this(defaultValue, Locale.getDefault(), locPattern);
139    }
140
141    /**
142     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
143     * that will return the specified default value
144     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
145     *
146     * @param defaultValue  The default value to be returned
147     * @param locale        The locale
148     */
149    public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale) {
150
151        this(defaultValue, locale, false);
152    }
153
154    /**
155     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
156     * that will return the specified default value
157     * if a conversion error occurs.
158     *
159     * @param defaultValue  The default value to be returned
160     * @param locale        The locale
161     * @param locPattern    Indicate whether the pattern is localized or not
162     */
163    public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) {
164
165        this(defaultValue, locale, null, locPattern);
166    }
167
168    /**
169     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
170     * that will return the specified default value
171     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
172     *
173     * @param defaultValue  The default value to be returned
174     * @param locale        The locale
175     * @param pattern       The convertion pattern
176     */
177    public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
178
179        this(defaultValue, locale, pattern, false);
180    }
181
182    /**
183     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
184     * that will return the specified default value
185     * if a conversion error occurs.
186     *
187     * @param defaultValue  The default value to be returned
188     * @param locale        The locale
189     * @param pattern       The convertion pattern
190     * @param locPattern    Indicate whether the pattern is localized or not
191     */
192    public SqlTimestampLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
193
194        super(defaultValue, locale, pattern, locPattern);
195    }
196
197    /**
198     * Convert the specified locale-sensitive input object into an output object of the
199     * specified type.
200     *
201     * @param value The input object to be converted
202     * @param pattern The pattern is used for the convertion
203     * @return The converted value
204     * @throws org.apache.commons.beanutils.ConversionException if conversion
205     * cannot be performed successfully
206     * @throws ParseException if an error occurs parsing a String to a Number
207     */
208    @Override
209    protected Object parse(final Object value, final String pattern) throws ParseException {
210
211        return new Timestamp(((java.util.Date) super.parse(value, pattern)).getTime());
212    }
213}