    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 *      http://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.Date;
    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.Date</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 SqlDateLocaleConverter 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 SqlDateLocaleConverter() {
    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 SqlDateLocaleConverter(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 SqlDateLocaleConverter(final Locale locale) {
    070
    071        this(locale, false);
    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 SqlDateLocaleConverter(final Locale locale, final boolean locPattern) {
    083
    084        this(locale, (String) null, locPattern);
    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 SqlDateLocaleConverter(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 SqlDateLocaleConverter(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 SqlDateLocaleConverter(final Object defaultValue) {
    124
    125        this(defaultValue, false);
    126    }
    127
    128    /**
    129     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    130     * that will return the specified default value
    131     * if a conversion error occurs. The locale is the default locale for
    132     * this instance of the Java Virtual Machine.
    133     *
    134     * @param defaultValue  The default value to be returned
    135     * @param locPattern    Indicate whether the pattern is localized or not
    136     */
    137    public SqlDateLocaleConverter(final Object defaultValue, final boolean locPattern) {
    138
    139        this(defaultValue, Locale.getDefault(), locPattern);
    140    }
    141
    142    /**
    143     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    144     * that will return the specified default value
    145     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    146     *
    147     * @param defaultValue  The default value to be returned
    148     * @param locale        The locale
    149     */
    150    public SqlDateLocaleConverter(final Object defaultValue, final Locale locale) {
    151
    152        this(defaultValue, locale, false);
    153    }
    154
    155    /**
    156     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    157     * that will return the specified default value
    158     * if a conversion error occurs.
    159     *
    160     * @param defaultValue  The default value to be returned
    161     * @param locale        The locale
    162     * @param locPattern    Indicate whether the pattern is localized or not
    163     */
    164    public SqlDateLocaleConverter(final Object defaultValue, final Locale locale, final boolean locPattern) {
    165
    166        this(defaultValue, locale, null, locPattern);
    167    }
    168
    169    /**
    170     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    171     * that will return the specified default value
    172     * if a conversion error occurs. An unlocalized pattern is used for the convertion.
    173     *
    174     * @param defaultValue  The default value to be returned
    175     * @param locale        The locale
    176     * @param pattern       The convertion pattern
    177     */
    178    public SqlDateLocaleConverter(final Object defaultValue, final Locale locale, final String pattern) {
    179
    180        this(defaultValue, locale, pattern, false);
    181    }
    182
    183    /**
    184     * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
    185     * that will return the specified default value
    186     * if a conversion error occurs.
    187     *
    188     * @param defaultValue  The default value to be returned
    189     * @param locale        The locale
    190     * @param pattern       The convertion pattern
    191     * @param locPattern    Indicate whether the pattern is localized or not
    192     */
    193    public SqlDateLocaleConverter(final Object defaultValue, final Locale locale, final String pattern, final boolean locPattern) {
    194
    195        super(defaultValue, locale, pattern, locPattern);
    196    }
    197
    198    /**
    199     * Convert the specified locale-sensitive input object into an output object of the
    200     * specified type.
    201     *
    202     * @param value The input object to be converted
    203     * @param pattern The pattern is used for the convertion
    204     * @return The converted value
    205     * @throws org.apache.commons.beanutils.ConversionException if conversion
    206     * cannot be performed successfully
    207     * @throws ParseException if an error occurs parsing a String to a Number
    208     */
    209    @Override
    210    protected Object parse(final Object value, final String pattern) throws ParseException {
    211
    212        return new Date(((java.util.Date) super.parse(value, pattern)).getTime());
    213    }
    214}