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.wicket.util.convert.converter;
018
019import java.sql.Timestamp;
020import java.text.DateFormat;
021import java.util.Locale;
022
023/**
024 * Converts to {@link Timestamp}.
025 * 
026 * @author eelcohillenius
027 */
028public class SqlTimestampConverter extends AbstractDateConverter<Timestamp>
029{
030        private static final long serialVersionUID = 1L;
031
032        private final int dateFormat;
033        private final int timeFormat;
034
035        /**
036         * Construct.
037         */
038        public SqlTimestampConverter()
039        {
040                this(DateFormat.SHORT, DateFormat.SHORT);
041        }
042
043        /**
044         * Construct.
045         * 
046         * @param dateFormat
047         *            See java.text.DateFormat for details. Defaults to DateFormat.SHORT
048         */
049        public SqlTimestampConverter(final int dateFormat)
050        {
051                this(dateFormat, DateFormat.SHORT);
052        }
053
054        /**
055         * Construct.
056         * 
057         * @param dateFormat
058         *            See java.text.DateFormat for details. Defaults to DateFormat.SHORT * @param
059         *            timeFormat See java.text.DateFormat for details. Defaults to DateFormat.SHORT
060         * @param timeFormat
061         */
062        public SqlTimestampConverter(final int dateFormat, final int timeFormat)
063        {
064                this.dateFormat = dateFormat;
065                this.timeFormat = timeFormat;
066        }
067
068        @Override
069        public DateFormat getDateFormat(Locale locale)
070        {
071                if (locale == null)
072                {
073                        locale = Locale.getDefault(Locale.Category.FORMAT);
074                }
075
076                // return a clone because DateFormat.getDateInstance uses a pool
077                return (DateFormat) DateFormat.getDateTimeInstance(dateFormat, timeFormat, locale).clone();
078        }
079
080        @Override
081        protected Timestamp createDateLike(long date)
082        {
083                return new Timestamp(date);
084        }
085
086        @Override
087        protected Class<Timestamp> getTargetType()
088        {
089                return Timestamp.class;
090        }
091
092}