Skip to content

Commit 62885a1

Browse files
committed
Revised format annotation docs
(cherry picked from commit aab4211)
1 parent 1b26f2f commit 62885a1

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,8 +26,8 @@
2626
* Declares that a field or method parameter should be formatted as a date or time.
2727
*
2828
* <p>Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
29-
* Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.lang.Long},
30-
* Joda-Time value types; and as of Spring 4 and JDK 8, to JSR-310 <code>java.time</code> types too.
29+
* Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code Long} (for
30+
* millisecond timestamps) as well as JSR-310 <code>java.time</code> and Joda-Time value types.
3131
*
3232
* <p>For style-based formatting, set the {@link #style} attribute to be the style pattern code.
3333
* The first character of the code is the date style, and the second character is the time style.
@@ -48,6 +48,7 @@
4848
* @author Keith Donald
4949
* @author Juergen Hoeller
5050
* @since 3.0
51+
* @see java.time.format.DateTimeFormatter
5152
* @see org.joda.time.format.DateTimeFormat
5253
*/
5354
@Documented

spring-context/src/main/java/org/springframework/format/annotation/NumberFormat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@
2525
/**
2626
* Declares that a field or method parameter should be formatted as a number.
2727
*
28-
* <p>Supports formatting by style or custom pattern string.
29-
* Can be applied to any JDK {@code java.lang.Number} type.
28+
* <p>Supports formatting by style or custom pattern string. Can be applied
29+
* to any JDK {@code Number} type such as {@code Double} and {@code Long}.
3030
*
3131
* <p>For style-based formatting, set the {@link #style} attribute to be the
3232
* desired {@link Style}. For custom formatting, set the {@link #pattern}

src/docs/asciidoc/core/core-validation.adoc

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,9 @@ the knowledge of how to convert properties to the desired type. Read more about
362362
A couple of examples where property editing is used in Spring:
363363

364364
* __setting properties on beans__ is done using `PropertyEditors`. When mentioning
365-
`java.lang.String` as the value of a property of some bean you're declaring in XML
366-
file, Spring will (if the setter of the corresponding property has a
367-
`Class`-parameter) use the `ClassEditor` to try to resolve the parameter to a `Class`
368-
object.
365+
`String` as the value of a property of some bean you're declaring in XML file,
366+
Spring will (if the setter of the corresponding property has `Class` parameter)
367+
use the `ClassEditor` to try to resolve the parameter to a `Class` object.
369368
* __parsing HTTP request parameters__ in Spring's MVC framework is done using all kinds
370369
of `PropertyEditors` that you can manually bind in all subclasses of the
371370
`CommandController`.
@@ -761,9 +760,9 @@ Consider `StringToInteger` as an example for a typical `Converter` implementatio
761760
[[core-convert-ConverterFactory-SPI]]
762761
=== ConverterFactory
763762

764-
When you need to centralize the conversion logic for an entire class hierarchy, for
765-
example, when converting from String to java.lang.Enum objects, implement
766-
`ConverterFactory`:
763+
When you need to centralize the conversion logic for an entire class hierarchy
764+
(for example, when converting from `String` to `Enum` objects), you can implement
765+
`ConverterFactory`, as the following example shows:
767766

768767
[source,java,indent=0]
769768
[subs="verbatim,quotes"]
@@ -777,10 +776,10 @@ example, when converting from String to java.lang.Enum objects, implement
777776
----
778777

779778
Parameterize S to be the type you are converting from and R to be the base type defining
780-
the __range__ of classes you can convert to. Then implement getConverter(Class<T>),
779+
the __range__ of classes you can convert to. Then implement `getConverter(Class<T>)`,
781780
where T is a subclass of R.
782781

783-
Consider the `StringToEnum` ConverterFactory as an example:
782+
Consider the `StringToEnumConverterFactory` as an example:
784783

785784
[source,java,indent=0]
786785
[subs="verbatim,quotes"]
@@ -813,12 +812,13 @@ Consider the `StringToEnum` ConverterFactory as an example:
813812
[[core-convert-GenericConverter-SPI]]
814813
=== GenericConverter
815814

816-
When you require a sophisticated Converter implementation, consider the GenericConverter
817-
interface. With a more flexible but less strongly typed signature, a GenericConverter
818-
supports converting between multiple source and target types. In addition, a
819-
GenericConverter makes available source and target field context you can use when
820-
implementing your conversion logic. Such context allows a type conversion to be driven
821-
by a field annotation, or generic information declared on a field signature.
815+
When you require a sophisticated `Converter` implementation, consider using the
816+
`GenericConverter` interface. With a more flexible but less strongly typed signature
817+
than `Converter`, a `GenericConverter` supports converting between multiple source and
818+
target types. In addition, a `GenericConverter` makes available source and target field
819+
context that you can use when you implement your conversion logic. Such context lets a
820+
type conversion be driven by a field annotation or by generic information declared on a
821+
field signature. The following listing shows the interface definition of `GenericConverter`:
822822

823823
[source,java,indent=0]
824824
[subs="verbatim,quotes"]
@@ -833,22 +833,22 @@ by a field annotation, or generic information declared on a field signature.
833833
}
834834
----
835835

836-
To implement a GenericConverter, have getConvertibleTypes() return the supported
837-
source->target type pairs. Then implement convert(Object, TypeDescriptor,
838-
TypeDescriptor) to implement your conversion logic. The source TypeDescriptor provides
839-
access to the source field holding the value being converted. The target TypeDescriptor
840-
provides access to the target field where the converted value will be set.
836+
To implement a `GenericConverter`, have `getConvertibleTypes()` return the supported
837+
source->target type pairs. Then implement `convert(Object, TypeDescriptor,
838+
TypeDescriptor)` to contain your conversion logic. The source `TypeDescriptor` provides
839+
access to the source field that holds the value being converted. The target `TypeDescriptor`
840+
provides access to the target field where the converted value is to be set.
841841

842-
A good example of a GenericConverter is a converter that converts between a Java Array
843-
and a Collection. Such an ArrayToCollectionConverter introspects the field that declares
844-
the target Collection type to resolve the Collection's element type. This allows each
845-
element in the source array to be converted to the Collection element type before the
846-
Collection is set on the target field.
842+
A good example of a `GenericConverter` is a converter that converts between a Java array
843+
and a collection. Such an `ArrayToCollectionConverter` introspects the field that declares
844+
the target collection type to resolve the collection's element type. This lets each
845+
element in the source array be converted to the collection element type before the
846+
collection is set on the target field.
847847

848848
[NOTE]
849849
====
850-
Because GenericConverter is a more complex SPI interface, only use it when you need it.
851-
Favor Converter or ConverterFactory for basic type conversion needs.
850+
Because `GenericConverter` is a more complex SPI interface, only use it when you need it.
851+
Favor `Converter` or `ConverterFactory` for basic type conversion needs.
852852
====
853853

854854

@@ -1044,11 +1044,11 @@ general __core.convert__ Converter SPI does not address such __formatting__ requ
10441044
directly. To directly address them, Spring 3 introduces a convenient Formatter SPI that
10451045
provides a simple and robust alternative to PropertyEditors for client environments.
10461046

1047-
In general, use the Converter SPI when you need to implement general-purpose type
1048-
conversion logic; for example, for converting between a java.util.Date and and
1049-
java.lang.Long. Use the Formatter SPI when you're working in a client environment, such
1050-
as a web application, and need to parse and print localized field values. The
1051-
ConversionService provides a unified type conversion API for both SPIs.
1047+
In general, you can use the `Converter` SPI when you need to implement general-purpose type
1048+
conversion logic -- for example, for converting between a `java.util.Date` and a `Long`.
1049+
You can use the `Formatter` SPI when you work in a client environment (such as a web
1050+
application) and need to parse and print localized field values. The `ConversionService`
1051+
provides a unified type conversion API for both SPIs.
10521052

10531053

10541054

@@ -1088,22 +1088,22 @@ Where Formatter extends from the Printer and Parser building-block interfaces:
10881088
}
10891089
----
10901090

1091-
To create your own Formatter, simply implement the Formatter interface above.
1092-
Parameterize T to be the type of object you wish to format, for example,
1093-
`java.util.Date`. Implement the `print()` operation to print an instance of T for
1091+
To create your own `Formatter`, implement the `Formatter` interface shown earlier.
1092+
Parameterize `T` to be the type of object you wish to format -- for example,
1093+
`java.util.Date`. Implement the `print()` operation to print an instance of `T` for
10941094
display in the client locale. Implement the `parse()` operation to parse an instance of
1095-
T from the formatted representation returned from the client locale. Your Formatter
1096-
should throw a ParseException or IllegalArgumentException if a parse attempt fails. Take
1097-
care to ensure your Formatter implementation is thread-safe.
1095+
`T` from the formatted representation returned from the client locale. Your `Formatter`
1096+
should throw a `ParseException` or an `IllegalArgumentException` if a parse attempt fails. Take
1097+
care to ensure that your `Formatter` implementation is thread-safe.
10981098

1099-
Several Formatter implementations are provided in `format` subpackages as a convenience.
1100-
The `number` package provides a `NumberStyleFormatter`, `CurrencyStyleFormatter`, and
1101-
`PercentStyleFormatter` to format `java.lang.Number` objects using a `java.text.NumberFormat`.
1099+
The `format` subpackages provide several `Formatter` implementations as a convenience.
1100+
The `number` package provides `NumberStyleFormatter`, `CurrencyStyleFormatter`, and
1101+
`PercentStyleFormatter` to format `Number` objects that use a `java.text.NumberFormat`.
11021102
The `datetime` package provides a `DateFormatter` to format `java.util.Date` objects with
11031103
a `java.text.DateFormat`. The `datetime.joda` package provides comprehensive datetime
11041104
formatting support based on the http://joda-time.sourceforge.net[Joda-Time library].
11051105

1106-
Consider `DateFormatter` as an example `Formatter` implementation:
1106+
The following `DateFormatter` is an example `Formatter` implementation:
11071107

11081108
[source,java,indent=0]
11091109
[subs="verbatim,quotes"]
@@ -1230,10 +1230,11 @@ To trigger formatting, simply annotate fields with @NumberFormat:
12301230
==== Format Annotation API
12311231

12321232
A portable format annotation API exists in the `org.springframework.format.annotation`
1233-
package. Use @NumberFormat to format java.lang.Number fields. Use @DateTimeFormat to
1234-
format java.util.Date, java.util.Calendar, java.util.Long, or Joda-Time fields.
1233+
package. You can use `@NumberFormat` to format `Number` fields such as `Double` and
1234+
`Long`, and `@DateTimeFormat` to format `java.util.Date`, `java.util.Calendar`, `Long`
1235+
(for millisecond timestamps) as well as JSR-310 `java.time` and Joda-Time value types.
12351236

1236-
The example below uses @DateTimeFormat to format a java.util.Date as a ISO Date
1237+
The following example uses `@DateTimeFormat` to format a `java.util.Date` as an ISO Date
12371238
(yyyy-MM-dd):
12381239

12391240
[source,java,indent=0]

0 commit comments

Comments
 (0)