1 /* 2 * Copyright 2002-2011 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.springframework.format.datetime.joda; 17 18 import java.util.Calendar; 19 import java.util.Date; 20 21 import org.joda.time.DateTime; 22 import org.joda.time.LocalDate; 23 import org.joda.time.LocalDateTime; 24 import org.joda.time.LocalTime; 25 import org.joda.time.ReadableInstant; 26 import org.joda.time.format.DateTimeFormat; 27 import org.joda.time.format.DateTimeFormatter; 28 import org.joda.time.format.ISODateTimeFormat; 29 import org.springframework.format.FormatterRegistrar; 30 import org.springframework.format.FormatterRegistry; 31 import org.springframework.format.Parser; 32 import org.springframework.format.Printer; 33 34 /** 35 * Configures Joda Time's Formatting system for use with Spring. 36 * 37 * @author Keith Donald 38 * @author Juergen Hoeller 39 * @since 3.1 40 * @see #setDateStyle 41 * @see #setTimeStyle 42 * @see #setDateTimeStyle 43 * @see #setUseIsoFormat 44 * @see #installJodaTimeFormatting 45 */ 46 public class JodaTimeFormatterRegistrar implements FormatterRegistrar { 47 48 private String dateStyle; 49 50 private String timeStyle; 51 52 private String dateTimeStyle; 53 54 private boolean useIsoFormat; 55 56 /** 57 * Set the default format style of Joda {@link LocalDate} objects. 58 * Default is {@link DateTimeFormat#shortDate()}. 59 */ setDateStyle(String dateStyle)60 public void setDateStyle(String dateStyle) { 61 this.dateStyle = dateStyle; 62 } 63 64 /** 65 * Set the default format style of Joda {@link LocalTime} objects. 66 * Default is {@link DateTimeFormat#shortTime()}. 67 */ setTimeStyle(String timeStyle)68 public void setTimeStyle(String timeStyle) { 69 this.timeStyle = timeStyle; 70 } 71 72 /** 73 * Set the default format style of Joda {@link LocalDateTime} and {@link DateTime} objects, 74 * as well as JDK {@link Date} and {@link Calendar} objects. 75 * Default is {@link DateTimeFormat#shortDateTime()}. 76 */ setDateTimeStyle(String dateTimeStyle)77 public void setDateTimeStyle(String dateTimeStyle) { 78 this.dateTimeStyle = dateTimeStyle; 79 } 80 81 /** 82 * Set whether standard ISO formatting should be applied to all Date/Time types. 83 * Default is false (no). 84 * If set to true, the dateStyle, timeStyle, and dateTimeStyle properties are ignored. 85 */ setUseIsoFormat(boolean useIsoFormat)86 public void setUseIsoFormat(boolean useIsoFormat) { 87 this.useIsoFormat = useIsoFormat; 88 } 89 registerFormatters(FormatterRegistry registry)90 public void registerFormatters(FormatterRegistry registry) { 91 JodaTimeConverters.registerConverters(registry); 92 93 DateTimeFormatter jodaDateFormatter = getJodaDateFormatter(); 94 registry.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(jodaDateFormatter), 95 new DateTimeParser(jodaDateFormatter)); 96 97 DateTimeFormatter jodaTimeFormatter = getJodaTimeFormatter(); 98 registry.addFormatterForFieldType(LocalTime.class, new ReadablePartialPrinter(jodaTimeFormatter), 99 new DateTimeParser(jodaTimeFormatter)); 100 101 DateTimeFormatter jodaDateTimeFormatter = getJodaDateTimeFormatter(); 102 Parser<DateTime> dateTimeParser = new DateTimeParser(jodaDateTimeFormatter); 103 registry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), 104 dateTimeParser); 105 106 Printer<ReadableInstant> readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter); 107 registry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser); 108 109 registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); 110 } 111 112 // internal helpers 113 getJodaDateFormatter()114 private DateTimeFormatter getJodaDateFormatter() { 115 if (this.useIsoFormat) { 116 return ISODateTimeFormat.date(); 117 } 118 if (this.dateStyle != null) { 119 return DateTimeFormat.forStyle(this.dateStyle + "-"); 120 } else { 121 return DateTimeFormat.shortDate(); 122 } 123 } 124 getJodaTimeFormatter()125 private DateTimeFormatter getJodaTimeFormatter() { 126 if (this.useIsoFormat) { 127 return ISODateTimeFormat.time(); 128 } 129 if (this.timeStyle != null) { 130 return DateTimeFormat.forStyle("-" + this.timeStyle); 131 } else { 132 return DateTimeFormat.shortTime(); 133 } 134 } 135 getJodaDateTimeFormatter()136 private DateTimeFormatter getJodaDateTimeFormatter() { 137 if (this.useIsoFormat) { 138 return ISODateTimeFormat.dateTime(); 139 } 140 if (this.dateTimeStyle != null) { 141 return DateTimeFormat.forStyle(this.dateTimeStyle); 142 } else { 143 return DateTimeFormat.shortDateTime(); 144 } 145 } 146 147 } 148