    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 */
    017package org.apache.commons.beanutils.converters;
    018
    019import java.sql.Timestamp;
    020import java.text.DateFormat;
    021import java.util.Locale;
    022import java.util.TimeZone;
    023
    024/**
    025 * {@link DateTimeConverter} implementation that handles conversion to
    026 * and from <strong>java.sql.Timestamp</strong> objects.
    027 * <p>
    028 * This implementation can be configured to handle conversion either
    029 * by using java.sql.Timestamp's default String conversion, or by using a
    030 * Locale's default format or by specifying a set of format patterns.
    031 * See the {@link DateTimeConverter} documentation for further details.
    032 * <p>
    033 * Can be configured to either return a <em>default value</em> or throw a
    034 * <code>ConversionException</code> if a conversion error occurs.
    035 *
    036 * @since 1.3
    037 */
    038public final class SqlTimestampConverter extends DateTimeConverter {
    039
    040    /**
    041     * Construct a <strong>java.sql.Timestamp</strong> <em>Converter</em> that throws
    042     * a <code>ConversionException</code> if an error occurs.
    043     */
    044    public SqlTimestampConverter() {
    045    }
    046
    047    /**
    048     * Construct a <strong>java.sql.Timestamp</strong> <em>Converter</em> that returns
    049     * a default value if an error occurs.
    050     *
    051     * @param defaultValue The default value to be returned
    052     * if the value to be converted is missing or an error
    053     * occurs converting the value.
    054     */
    055    public SqlTimestampConverter(final Object defaultValue) {
    056        super(defaultValue);
    057    }
    058
    059    /**
    060     * Return the default type this <code>Converter</code> handles.
    061     *
    062     * @return The default type this <code>Converter</code> handles.
    063     * @since 1.8.0
    064     */
    065    @Override
    066    protected Class<?> getDefaultType() {
    067        return Timestamp.class;
    068    }
    069
    070    /**
    071     * Return a <code>DateFormat</code> for the Locale.
    072     * @param locale TODO
    073     * @param timeZone TODO
    074     * @return The DateFormat.
    075     * @since 1.8.0
    076     */
    077    @Override
    078    protected DateFormat getFormat(final Locale locale, final TimeZone timeZone) {
    079        DateFormat format = null;
    080        if (locale == null) {
    081            format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    082        } else {
    083            format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
    084        }
    085        if (timeZone != null) {
    086            format.setTimeZone(timeZone);
    087        }
    088        return format;
    089    }
    090}