Skip to content

Commit c5f3915

Browse files
committed
Document global date time format configuration
Update reference documentation to detail how global date and time formats can be specified. Issue: SPR-9952
1 parent fddb829 commit c5f3915

File tree

1 file changed

+93
-2
lines changed

1 file changed

+93
-2
lines changed

src/reference/docbook/validation.xml

+93-2
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ public interface ConversionService {
11011101
</bean>]]></programlisting>
11021102

11031103
<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"/>
11051105
for details on use with
11061106
<literal>&lt;mvc:annotation-driven/&gt;</literal>.</para>
11071107

@@ -1415,7 +1415,7 @@ public interface FormatterRegistrar {
14151415
</para>
14161416
</section>
14171417

1418-
<section id="format-configuring-FormattingConversionService">
1418+
<section id="format-configuring-formatting-mvc">
14191419
<title>Configuring Formatting in Spring MVC</title>
14201420

14211421
<para> In a Spring MVC application, you may configure a custom
@@ -1498,6 +1498,97 @@ public interface FormatterRegistrar {
14981498
</section>
14991499
</section>
15001500

1501+
<section id="format-configuring-formatting-globaldatetimeformat">
1502+
<title>Configuring a global date &amp; 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+
15011592
<section id="validation-beanvalidation">
15021593
<title>Spring 3 Validation</title>
15031594

0 commit comments

Comments
 (0)