Skip to content

Commit 5c8043d

Browse files
committed
Use code includes and tabs in date/time format documentation
See spring-projectsgh-22171
1 parent 13df59c commit 5c8043d

File tree

4 files changed

+141
-99
lines changed

4 files changed

+141
-99
lines changed

framework-docs/modules/ROOT/pages/core/validation/format-configuring-formatting-globaldatetimeformat.adoc

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -11,106 +11,9 @@ formatters manually with the help of:
1111
* `org.springframework.format.datetime.standard.DateTimeFormatterRegistrar`
1212
* `org.springframework.format.datetime.DateFormatterRegistrar`
1313

14-
For example, the following Java configuration registers a global `yyyyMMdd` format:
14+
For example, the following configuration registers a global `yyyyMMdd` format:
1515

16-
[tabs]
17-
======
18-
Java::
19-
+
20-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
21-
----
22-
@Configuration
23-
public class AppConfig {
24-
25-
@Bean
26-
public FormattingConversionService conversionService() {
27-
28-
// Use the DefaultFormattingConversionService but do not register defaults
29-
DefaultFormattingConversionService conversionService =
30-
new DefaultFormattingConversionService(false);
31-
32-
// Ensure @NumberFormat is still supported
33-
conversionService.addFormatterForFieldAnnotation(
34-
new NumberFormatAnnotationFormatterFactory());
35-
36-
// Register JSR-310 date conversion with a specific global format
37-
DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
38-
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
39-
dateTimeRegistrar.registerFormatters(conversionService);
40-
41-
// Register date conversion with a specific global format
42-
DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
43-
dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd"));
44-
dateRegistrar.registerFormatters(conversionService);
45-
46-
return conversionService;
47-
}
48-
}
49-
----
50-
51-
Kotlin::
52-
+
53-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
54-
----
55-
@Configuration
56-
class AppConfig {
57-
58-
@Bean
59-
fun conversionService(): FormattingConversionService {
60-
// Use the DefaultFormattingConversionService but do not register defaults
61-
return DefaultFormattingConversionService(false).apply {
62-
63-
// Ensure @NumberFormat is still supported
64-
addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory())
65-
66-
// Register JSR-310 date conversion with a specific global format
67-
val dateTimeRegistrar = DateTimeFormatterRegistrar()
68-
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"))
69-
dateTimeRegistrar.registerFormatters(this)
70-
71-
// Register date conversion with a specific global format
72-
val dateRegistrar = DateFormatterRegistrar()
73-
dateRegistrar.setFormatter(DateFormatter("yyyyMMdd"))
74-
dateRegistrar.registerFormatters(this)
75-
}
76-
}
77-
}
78-
----
79-
======
80-
81-
If you prefer XML-based configuration, you can use a
82-
`FormattingConversionServiceFactoryBean`. The following example shows how to do so:
83-
84-
[source,xml,indent=0,subs="verbatim,quotes"]
85-
----
86-
<?xml version="1.0" encoding="UTF-8"?>
87-
<beans xmlns="http://www.springframework.org/schema/beans"
88-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
89-
xsi:schemaLocation="
90-
http://www.springframework.org/schema/beans
91-
https://www.springframework.org/schema/beans/spring-beans.xsd">
92-
93-
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
94-
<property name="registerDefaultFormatters" value="false" />
95-
<property name="formatters">
96-
<set>
97-
<bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
98-
</set>
99-
</property>
100-
<property name="formatterRegistrars">
101-
<set>
102-
<bean class="org.springframework.format.datetime.standard.DateTimeFormatterRegistrar">
103-
<property name="dateFormatter">
104-
<bean class="org.springframework.format.datetime.standard.DateTimeFormatterFactoryBean">
105-
<property name="pattern" value="yyyyMMdd"/>
106-
</bean>
107-
</property>
108-
</bean>
109-
</set>
110-
</property>
111-
</bean>
112-
</beans>
113-
----
16+
include-code::./ApplicationConfiguration[tag=snippet,indent=0]
11417

11518
Note there are extra considerations when configuring date and time formats in web
11619
applications. Please see
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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+
17+
package org.springframework.docs.core.validation.formatconfiguringformattingglobaldatetimeformat;
18+
19+
import java.time.format.DateTimeFormatter;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.format.datetime.DateFormatter;
24+
import org.springframework.format.datetime.DateFormatterRegistrar;
25+
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
26+
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
27+
import org.springframework.format.support.DefaultFormattingConversionService;
28+
import org.springframework.format.support.FormattingConversionService;
29+
30+
// tag::snippet[]
31+
@Configuration
32+
public class ApplicationConfiguration {
33+
34+
@Bean
35+
public FormattingConversionService conversionService() {
36+
37+
// Use the DefaultFormattingConversionService but do not register defaults
38+
DefaultFormattingConversionService conversionService =
39+
new DefaultFormattingConversionService(false);
40+
41+
// Ensure @NumberFormat is still supported
42+
conversionService.addFormatterForFieldAnnotation(
43+
new NumberFormatAnnotationFormatterFactory());
44+
45+
// Register JSR-310 date conversion with a specific global format
46+
DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
47+
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
48+
dateTimeRegistrar.registerFormatters(conversionService);
49+
50+
// Register date conversion with a specific global format
51+
DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
52+
dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd"));
53+
dateRegistrar.registerFormatters(conversionService);
54+
55+
return conversionService;
56+
}
57+
}
58+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2023 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+
* https://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.docs.core.validation.formatconfiguringformattingglobaldatetimeformat
17+
18+
import org.springframework.context.annotation.Bean
19+
import org.springframework.context.annotation.Configuration
20+
import org.springframework.format.datetime.DateFormatter
21+
import org.springframework.format.datetime.DateFormatterRegistrar
22+
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar
23+
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory
24+
import org.springframework.format.support.DefaultFormattingConversionService
25+
import org.springframework.format.support.FormattingConversionService
26+
import java.time.format.DateTimeFormatter
27+
28+
// tag::snippet[]
29+
@Configuration
30+
class ApplicationConfiguration {
31+
32+
@Bean
33+
fun conversionService(): FormattingConversionService {
34+
// Use the DefaultFormattingConversionService but do not register defaults
35+
return DefaultFormattingConversionService(false).apply {
36+
37+
// Ensure @NumberFormat is still supported
38+
addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory())
39+
40+
// Register JSR-310 date conversion with a specific global format
41+
val dateTimeRegistrar = DateTimeFormatterRegistrar()
42+
dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"))
43+
dateTimeRegistrar.registerFormatters(this)
44+
45+
// Register date conversion with a specific global format
46+
val dateRegistrar = DateFormatterRegistrar()
47+
dateRegistrar.setFormatter(DateFormatter("yyyyMMdd"))
48+
dateRegistrar.registerFormatters(this)
49+
}
50+
}
51+
}
52+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans
6+
https://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<!-- tag::snippet[] -->
9+
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
10+
<property name="registerDefaultFormatters" value="false" />
11+
<property name="formatters">
12+
<set>
13+
<bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" />
14+
</set>
15+
</property>
16+
<property name="formatterRegistrars">
17+
<set>
18+
<bean class="org.springframework.format.datetime.standard.DateTimeFormatterRegistrar">
19+
<property name="dateFormatter">
20+
<bean class="org.springframework.format.datetime.standard.DateTimeFormatterFactoryBean">
21+
<property name="pattern" value="yyyyMMdd"/>
22+
</bean>
23+
</property>
24+
</bean>
25+
</set>
26+
</property>
27+
</bean>
28+
<!-- end::snippet[] -->
29+
</beans>

0 commit comments

Comments
 (0)