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.text.DateFormat;
020import java.util.Date;
021import java.util.Locale;
022
023import org.apache.wicket.util.string.Strings;
024
025/**
026 * A base class for all Date related converters
027 *
028 * @param <D>
029 *     the type of the Date that is supported by this converter
030 */
031public abstract class AbstractDateConverter<D extends Date> extends AbstractConverter<D>
032{
033        private static final long serialVersionUID = 1L;
034
035        /**
036         * Creates a new instance of D out of the passed date(time) as long
037         * @param date
038         *      the date(time) in millis since Epoch
039         * @return a new instance of the specific type D
040         */
041        protected abstract D createDateLike(long date);
042
043        @Override
044        public D convertToObject(final String value, Locale locale)
045        {
046                if (Strings.isEmpty(value))
047                {
048                        return null;
049                }
050
051                DateFormat format = getDateFormat(locale);
052                Date date = parse(format, value, locale);
053                return createDateLike(date.getTime());
054        }
055
056        @Override
057        public String convertToString(final D value, final Locale locale)
058        {
059                if (value == null)
060                {
061                        return null;
062                }
063
064                final DateFormat dateFormat = getDateFormat(locale);
065                if (dateFormat != null)
066                {
067                        return dateFormat.format(value);
068                }
069                return value.toString();
070        }
071
072        /**
073         * @param locale
074         * @return Returns the date format.
075         */
076        public DateFormat getDateFormat(Locale locale)
077        {
078                if (locale == null)
079                {
080                        locale = Locale.getDefault(Locale.Category.FORMAT);
081                }
082
083                // return a clone because DateFormat.getDateInstance uses a pool
084                return (DateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale).clone();
085        }
086}