@@ -1101,7 +1101,7 @@ public interface ConversionService {
1101
1101
</bean>]]> </programlisting >
1102
1102
1103
1103
<para >It is also common to use a ConversionService within a Spring MVC
1104
- application. See <xref linkend =" format-configuring-FormattingConversionService " />
1104
+ application. See <xref linkend =" format-configuring-formatting-mvc " />
1105
1105
for details on use with
1106
1106
<literal >< mvc:annotation-driven/> </literal >.</para >
1107
1107
@@ -1415,7 +1415,7 @@ public interface FormatterRegistrar {
1415
1415
</para >
1416
1416
</section >
1417
1417
1418
- <section id =" format-configuring-FormattingConversionService " >
1418
+ <section id =" format-configuring-formatting-mvc " >
1419
1419
<title >Configuring Formatting in Spring MVC</title >
1420
1420
1421
1421
<para > In a Spring MVC application, you may configure a custom
@@ -1498,6 +1498,97 @@ public interface FormatterRegistrar {
1498
1498
</section >
1499
1499
</section >
1500
1500
1501
+ <section id =" format-configuring-formatting-globaldatetimeformat" >
1502
+ <title >Configuring a global date & time format</title >
1503
+
1504
+ <para >By default, date and time fields that are not annotated with
1505
+ <interfacename >@DateTimeFormat</interfacename > are converted from strings
1506
+ using the the <literal >DateFormat.SHORT</literal > style. If you prefer,
1507
+ you can change this by defining your own global format.</para >
1508
+
1509
+ <para >You will need to ensure that Spring does not register default
1510
+ formatters and instead you should register all formatters manually. Use the
1511
+ <classname >org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar</classname >
1512
+ or <classname >org.springframework.format.datetime.DateFormatterRegistrar</classname >
1513
+ class depending on your use of the Joda Time library.</para >
1514
+
1515
+ <para >For example, the following Java configuration will register a global
1516
+ '<literal >yyyyMMdd</literal >' format. This example does not depend on the
1517
+ Joda Time:</para >
1518
+ <programlisting language =" java" >@Configurable
1519
+ public class AppConfig {
1520
+
1521
+ @Bean
1522
+ public FormattingConversionService conversionService() {
1523
+
1524
+ // Use the DefaultFormattingConversionService but do not register defaults
1525
+ DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
1526
+
1527
+ // Ensure @NumberFormat is still supported
1528
+ conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
1529
+
1530
+ // Register date conversion with a specific global format
1531
+ DateFormatterRegistrar registrar = new DateFormatterRegistrar();
1532
+ registrar.setFormatter(new DateFormatter("yyyyMMdd"));
1533
+ registrar.registerFormatters(conversionService);
1534
+
1535
+ return conversionService;
1536
+ }
1537
+ }</programlisting >
1538
+
1539
+ <para >If you prefer XML based configuration you can use a
1540
+ <classname >FormattingConversionServiceFactoryBean</classname >. Here is the same
1541
+ example, this time using Joda Time:</para >
1542
+ <programlisting language =" xml" ><![CDATA[ <?xml version="1.0" encoding="UTF-8"?>
1543
+ <beans xmlns="http://www.springframework.org/schema/beans"
1544
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1545
+ xsi:schemaLocation="
1546
+ http://www.springframework.org/schema/beans
1547
+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>
1548
+
1549
+ <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
1550
+ <property name="registerDefaultFormatters" value="false" />
1551
+ <property name="formatters">
1552
+ <set>
1553
+ <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
1554
+ </set>
1555
+ </property>
1556
+ <property name="formatterRegistrars">
1557
+ <set>
1558
+ <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar">
1559
+ <property name="dateFormatter">
1560
+ <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean">
1561
+ <property name="pattern" value="yyyyMMdd"/>
1562
+ </bean>
1563
+ </property>
1564
+ </bean>
1565
+ </set>
1566
+ </property>
1567
+ </bean>
1568
+ </beans>
1569
+ ]]> </programlisting >
1570
+
1571
+ <note >
1572
+ <para >Joda Time provides separate distinct types to represent
1573
+ <literal >date</literal >, <literal >time</literal > and
1574
+ <literal >date-time</literal > values. The <literal >dateFormatter</literal >
1575
+ , <literal >timeFormatter</literal > and <literal >dateTimeFormatter</literal >
1576
+ properties of the <classname >JodaTimeFormatterRegistrar</classname > should
1577
+ be used to configure the different formats for each type. The
1578
+ <classname >DateTimeFormatterFactoryBean</classname > provides a
1579
+ convenient way to create formatters.</para >
1580
+ </note >
1581
+
1582
+ <para >If you are using Spring MVC remember to explicitly configure the
1583
+ conversion service that is used. For Java based
1584
+ <interfacename >@Configuration</interfacename > this means extending the
1585
+ <classname >WebMvcConfigurationSupport</classname > class and overriding
1586
+ the <literal >mvcConversionService()</literal > method. For XML you should
1587
+ use the <literal >'conversion-service'</literal > attribute of the
1588
+ <literal >mvc:annotation-driven</literal > element. See
1589
+ <xref linkend =" format-configuring-formatting-mvc" /> for details.</para >
1590
+ </section >
1591
+
1501
1592
<section id =" validation-beanvalidation" >
1502
1593
<title >Spring 3 Validation</title >
1503
1594
0 commit comments