001/* 002 * Copyright 2002-2017 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.lang.Nullable; 027import org.springframework.util.Assert; 028 029/** 030 * Configures basic date formatting for use with Spring, primarily for 031 * {@link org.springframework.format.annotation.DateTimeFormat} declarations. 032 * Applies to fields of type {@link Date}, {@link Calendar} and {@code long}. 033 * 034 * <p>Designed for direct instantiation but also exposes the static 035 * {@link #addDateConverters(ConverterRegistry)} utility method for 036 * ad-hoc use against any {@code ConverterRegistry} instance. 037 * 038 * @author Phillip Webb 039 * @since 3.2 040 * @see org.springframework.format.datetime.standard.DateTimeFormatterRegistrar 041 * @see org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar 042 * @see FormatterRegistrar#registerFormatters 043 */ 044public class DateFormatterRegistrar implements FormatterRegistrar { 045 046 @Nullable 047 private DateFormatter dateFormatter; 048 049 050 /** 051 * Set a global date formatter to register. 052 * <p>If not specified, no general formatter for non-annotated 053 * {@link Date} and {@link Calendar} fields will be registered. 054 */ 055 public void setFormatter(DateFormatter dateFormatter) { 056 Assert.notNull(dateFormatter, "DateFormatter must not be null"); 057 this.dateFormatter = dateFormatter; 058 } 059 060 061 @Override 062 public void registerFormatters(FormatterRegistry registry) { 063 addDateConverters(registry); 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 registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); 071 } 072 073 /** 074 * Add date converters to the specified registry. 075 * @param converterRegistry the registry of converters to add to 076 */ 077 public static void addDateConverters(ConverterRegistry converterRegistry) { 078 converterRegistry.addConverter(new DateToLongConverter()); 079 converterRegistry.addConverter(new DateToCalendarConverter()); 080 converterRegistry.addConverter(new CalendarToDateConverter()); 081 converterRegistry.addConverter(new CalendarToLongConverter()); 082 converterRegistry.addConverter(new LongToDateConverter()); 083 converterRegistry.addConverter(new LongToCalendarConverter()); 084 } 085 086 087 private static class DateToLongConverter implements Converter<Date, Long> { 088 089 @Override 090 public Long convert(Date source) { 091 return source.getTime(); 092 } 093 } 094 095 096 private static class DateToCalendarConverter implements Converter<Date, Calendar> { 097 098 @Override 099 public Calendar convert(Date source) { 100 Calendar calendar = Calendar.getInstance(); 101 calendar.setTime(source); 102 return calendar; 103 } 104 } 105 106 107 private static class CalendarToDateConverter implements Converter<Calendar, Date> { 108 109 @Override 110 public Date convert(Calendar source) { 111 return source.getTime(); 112 } 113 } 114 115 116 private static class CalendarToLongConverter implements Converter<Calendar, Long> { 117 118 @Override 119 public Long convert(Calendar source) { 120 return source.getTimeInMillis(); 121 } 122 } 123 124 125 private static class LongToDateConverter implements Converter<Long, Date> { 126 127 @Override 128 public Date convert(Long source) { 129 return new Date(source); 130 } 131 } 132 133 134 private static class LongToCalendarConverter implements Converter<Long, Calendar> { 135 136 @Override 137 public Calendar convert(Long source) { 138 Calendar calendar = Calendar.getInstance(); 139 calendar.setTimeInMillis(source); 140 return calendar; 141 } 142 } 143 144}