Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
chenpeng_fei
spring-framework
提交
1ae17c27
S
spring-framework
项目概览
chenpeng_fei
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1ae17c27
编写于
11月 30, 2016
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Efficient ISO_LOCAL_* variants for printing LocalDate/LocalTime/LocalDateTime
Issue: SPR-14958
上级
5458a427
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
72 addition
and
28 deletion
+72
-28
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java
.../format/datetime/standard/DateTimeFormatterRegistrar.java
+24
-20
spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java
...ndard/Jsr310DateTimeFormatAnnotationFormatterFactory.java
+23
-1
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
...ork/format/datetime/standard/DateTimeFormattingTests.java
+25
-7
未找到文件。
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java
浏览文件 @
1ae17c27
...
...
@@ -29,7 +29,7 @@ import java.time.YearMonth;
import
java.time.ZonedDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.FormatStyle
;
import
java.util.
Hash
Map
;
import
java.util.
Enum
Map
;
import
java.util.Map
;
import
org.springframework.format.FormatterRegistrar
;
...
...
@@ -56,18 +56,17 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar {
/**
* User
defined formatters.
* User
-
defined formatters.
*/
private
final
Map
<
Type
,
DateTimeFormatter
>
formatters
=
new
HashMap
<>(
);
private
final
Map
<
Type
,
DateTimeFormatter
>
formatters
=
new
EnumMap
<>(
Type
.
class
);
/**
* Factories used when specific formatters have not been specified.
*/
private
final
Map
<
Type
,
DateTimeFormatterFactory
>
factories
;
private
final
Map
<
Type
,
DateTimeFormatterFactory
>
factories
=
new
EnumMap
<>(
Type
.
class
)
;
public
DateTimeFormatterRegistrar
()
{
this
.
factories
=
new
HashMap
<>();
for
(
Type
type
:
Type
.
values
())
{
this
.
factories
.
put
(
type
,
new
DateTimeFormatterFactory
());
}
...
...
@@ -155,33 +154,38 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar {
public
void
registerFormatters
(
FormatterRegistry
registry
)
{
DateTimeConverters
.
registerConverters
(
registry
);
DateTimeFormatter
dateFormatter
=
getFormatter
(
Type
.
DATE
);
DateTimeFormatter
timeFormatter
=
getFormatter
(
Type
.
TIME
);
DateTimeFormatter
dateTimeFormatter
=
getFormatter
(
Type
.
DATE_TIME
);
DateTimeFormatter
df
=
getFormatter
(
Type
.
DATE
);
DateTimeFormatter
tf
=
getFormatter
(
Type
.
TIME
);
DateTimeFormatter
dtf
=
getFormatter
(
Type
.
DATE_TIME
);
// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
registry
.
addFormatterForFieldType
(
LocalDate
.
class
,
new
TemporalAccessorPrinter
(
dateFormatter
),
new
TemporalAccessorParser
(
LocalDate
.
class
,
dateFormatter
));
new
TemporalAccessorPrinter
(
df
==
DateTimeFormatter
.
ISO_DATE
?
DateTimeFormatter
.
ISO_LOCAL_DATE
:
df
),
new
TemporalAccessorParser
(
LocalDate
.
class
,
df
));
registry
.
addFormatterForFieldType
(
LocalTime
.
class
,
new
TemporalAccessorPrinter
(
timeFormatter
),
new
TemporalAccessorParser
(
LocalTime
.
class
,
timeFormatter
));
new
TemporalAccessorPrinter
(
tf
==
DateTimeFormatter
.
ISO_TIME
?
DateTimeFormatter
.
ISO_LOCAL_TIME
:
tf
),
new
TemporalAccessorParser
(
LocalTime
.
class
,
tf
));
registry
.
addFormatterForFieldType
(
LocalDateTime
.
class
,
new
TemporalAccessorPrinter
(
dateTimeFormatter
),
new
TemporalAccessorParser
(
LocalDateTime
.
class
,
dateTimeFormatter
));
new
TemporalAccessorPrinter
(
dtf
==
DateTimeFormatter
.
ISO_DATE_TIME
?
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
:
dtf
),
new
TemporalAccessorParser
(
LocalDateTime
.
class
,
dtf
));
registry
.
addFormatterForFieldType
(
ZonedDateTime
.
class
,
new
TemporalAccessorPrinter
(
d
ateTimeFormatter
),
new
TemporalAccessorParser
(
ZonedDateTime
.
class
,
d
ateTimeFormatter
));
new
TemporalAccessorPrinter
(
d
tf
),
new
TemporalAccessorParser
(
ZonedDateTime
.
class
,
d
tf
));
registry
.
addFormatterForFieldType
(
OffsetDateTime
.
class
,
new
TemporalAccessorPrinter
(
d
ateTimeFormatter
),
new
TemporalAccessorParser
(
OffsetDateTime
.
class
,
d
ateTimeFormatter
));
new
TemporalAccessorPrinter
(
d
tf
),
new
TemporalAccessorParser
(
OffsetDateTime
.
class
,
d
tf
));
registry
.
addFormatterForFieldType
(
OffsetTime
.
class
,
new
TemporalAccessorPrinter
(
t
imeFormatter
),
new
TemporalAccessorParser
(
OffsetTime
.
class
,
t
imeFormatter
));
new
TemporalAccessorPrinter
(
t
f
),
new
TemporalAccessorParser
(
OffsetTime
.
class
,
t
f
));
registry
.
addFormatterForFieldType
(
Instant
.
class
,
new
InstantFormatter
());
registry
.
addFormatterForFieldType
(
Period
.
class
,
new
PeriodFormatter
());
...
...
spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java
浏览文件 @
1ae17c27
...
...
@@ -68,6 +68,24 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
@Override
public
Printer
<?>
getPrinter
(
DateTimeFormat
annotation
,
Class
<?>
fieldType
)
{
DateTimeFormatter
formatter
=
getFormatter
(
annotation
,
fieldType
);
// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
if
(
formatter
==
DateTimeFormatter
.
ISO_DATE
)
{
if
(
isLocal
(
fieldType
))
{
formatter
=
DateTimeFormatter
.
ISO_LOCAL_DATE
;
}
}
else
if
(
formatter
==
DateTimeFormatter
.
ISO_TIME
)
{
if
(
isLocal
(
fieldType
))
{
formatter
=
DateTimeFormatter
.
ISO_LOCAL_TIME
;
}
}
else
if
(
formatter
==
DateTimeFormatter
.
ISO_DATE_TIME
)
{
if
(
isLocal
(
fieldType
))
{
formatter
=
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
;
}
}
return
new
TemporalAccessorPrinter
(
formatter
);
}
...
...
@@ -81,7 +99,7 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
/**
* Factory method used to create a {@link DateTimeFormatter}.
* @param annotation the format annotation for the field
* @param fieldType the
type of
field
* @param fieldType the
declared type of the
field
* @return a {@link DateTimeFormatter} instance
*/
protected
DateTimeFormatter
getFormatter
(
DateTimeFormat
annotation
,
Class
<?>
fieldType
)
{
...
...
@@ -92,4 +110,8 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
return
factory
.
createDateTimeFormatter
();
}
private
boolean
isLocal
(
Class
<?>
fieldType
)
{
return
fieldType
.
getSimpleName
().
startsWith
(
"Local"
);
}
}
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
浏览文件 @
1ae17c27
...
...
@@ -311,6 +311,15 @@ public class DateTimeFormattingTests {
@Test
public
void
testBindISOTime
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"isoTime"
,
"12:00:00"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertEquals
(
"12:00:00"
,
binder
.
getBindingResult
().
getFieldValue
(
"isoTime"
));
}
@Test
public
void
testBindISOTimeWithZone
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"isoTime"
,
"12:00:00.000-05:00"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -320,6 +329,15 @@ public class DateTimeFormattingTests {
@Test
public
void
testBindISODateTime
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"isoDateTime"
,
"2009-10-31T12:00:00"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertEquals
(
"2009-10-31T12:00:00"
,
binder
.
getBindingResult
().
getFieldValue
(
"isoDateTime"
));
}
@Test
public
void
testBindISODateTimeWithZone
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"isoDateTime"
,
"2009-10-31T12:00:00.000Z"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -386,29 +404,29 @@ public class DateTimeFormattingTests {
private
LocalDate
localDate
;
@DateTimeFormat
(
style
=
"M-"
)
@DateTimeFormat
(
style
=
"M-"
)
private
LocalDate
localDateAnnotated
;
private
LocalTime
localTime
;
@DateTimeFormat
(
style
=
"-M"
)
@DateTimeFormat
(
style
=
"-M"
)
private
LocalTime
localTimeAnnotated
;
private
LocalDateTime
localDateTime
;
@DateTimeFormat
(
style
=
"MM"
)
@DateTimeFormat
(
style
=
"MM"
)
private
LocalDateTime
localDateTimeAnnotated
;
@DateTimeFormat
(
pattern
=
"M/d/yy h:mm a"
)
@DateTimeFormat
(
pattern
=
"M/d/yy h:mm a"
)
private
LocalDateTime
dateTimeAnnotatedPattern
;
@DateTimeFormat
(
iso
=
ISO
.
DATE
)
@DateTimeFormat
(
iso
=
ISO
.
DATE
)
private
LocalDate
isoDate
;
@DateTimeFormat
(
iso
=
ISO
.
TIME
)
@DateTimeFormat
(
iso
=
ISO
.
TIME
)
private
LocalTime
isoTime
;
@DateTimeFormat
(
iso
=
ISO
.
DATE_TIME
)
@DateTimeFormat
(
iso
=
ISO
.
DATE_TIME
)
private
LocalDateTime
isoDateTime
;
private
Instant
instant
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录