001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.datetime;
018
019import java.util.Calendar;
020import java.util.Date;
021
022import org.springframework.core.convert.converter.Converter;
023import org.springframework.core.convert.converter.ConverterRegistry;
024import org.springframework.format.FormatterRegistrar;
025import org.springframework.format.FormatterRegistry;
026import org.springframework.util.Assert;
027
028/**
029 * Configures basic date formatting for use with Spring, primarily for
030 * {@link org.springframework.format.annotation.DateTimeFormat} declarations.
031 * Applies to fields of type {@link Date}, {@link Calendar} and {@code long}.
032 *
033 * <p>Designed for direct instantiation but also exposes the static
034 * {@link #addDateConverters(ConverterRegistry)} utility method for
035 * ad-hoc use against any {@code ConverterRegistry} instance.
036 *
037 * @author Phillip Webb
038 * @since 3.2
039 * @see org.springframework.format.datetime.standard.DateTimeFormatterRegistrar
040 * @see org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar
041 * @see FormatterRegistrar#registerFormatters
042 */
043public class DateFormatterRegistrar implements FormatterRegistrar {
044
045        private DateFormatter dateFormatter;
046
047
048        /**
049         * Set a global date formatter to register.
050         * <p>If not specified, no general formatter for non-annotated
051         * {@link Date} and {@link Calendar} fields will be registered.
052         */
053        public void setFormatter(DateFormatter dateFormatter) {
054                Assert.notNull(dateFormatter, "DateFormatter must not be null");
055                this.dateFormatter = dateFormatter;
056        }
057
058
059        @Override
060        public void registerFormatters(FormatterRegistry registry) {
061                addDateConverters(registry);
062                registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
063
064                // In order to retain back compatibility we only register Date/Calendar
065                // types when a user defined formatter is specified (see SPR-10105)
066                if (this.dateFormatter != null) {
067                        registry.addFormatter(this.dateFormatter);
068                        registry.addFormatterForFieldType(Calendar.class, this.dateFormatter);
069                }
070        }
071
072        /**
073         * Add date converters to the specified registry.
074         * @param converterRegistry the registry of converters to add to
075         */
076        public static void addDateConverters(ConverterRegistry converterRegistry) {
077                converterRegistry.addConverter(new DateToLongConverter());
078                converterRegistry.addConverter(new DateToCalendarConverter());
079                converterRegistry.addConverter(new CalendarToDateConverter());
080                converterRegistry.addConverter(new CalendarToLongConverter());
081                converterRegistry.addConverter(new LongToDateConverter());
082                converterRegistry.addConverter(new LongToCalendarConverter());
083        }
084
085
086        private static class DateToLongConverter implements Converter<Date, Long> {
087
088                @Override
089                public Long convert(Date source) {
090                        return source.getTime();
091                }
092        }
093
094
095        private static class DateToCalendarConverter implements Converter<Date, Calendar> {
096
097                @Override
098                public Calendar convert(Date source) {
099                        Calendar calendar = Calendar.getInstance();
100                        calendar.setTime(source);
101                        return calendar;
102                }
103        }
104
105
106        private static class CalendarToDateConverter implements Converter<Calendar, Date> {
107
108                @Override
109                public Date convert(Calendar source) {
110                        return source.getTime();
111                }
112        }
113
114
115        private static class CalendarToLongConverter implements Converter<Calendar, Long> {
116
117                @Override
118                public Long convert(Calendar source) {
119                        return source.getTimeInMillis();
120                }
121        }
122
123
124        private static class LongToDateConverter implements Converter<Long, Date> {
125
126                @Override
127                public Date convert(Long source) {
128                        return new Date(source);
129                }
130        }
131
132
133        private static class LongToCalendarConverter implements Converter<Long, Calendar> {
134
135                @Override
136                public Calendar convert(Long source) {
137                        Calendar calendar = Calendar.getInstance();
138                        calendar.setTimeInMillis(source);
139                        return calendar;
140                }
141        }
142
143}