Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
d5ca5b74
S
spring-framework
项目概览
带水的鱼儿
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
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,发现更多精彩内容 >>
提交
d5ca5b74
编写于
11月 25, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
DateTimeFormatterRegistrar supports Duration and Period as well
Issue: SPR-13721
上级
b35d44bd
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
158 addition
and
2 deletion
+158
-2
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java
...ramework/format/datetime/standard/DateTimeConverters.java
+18
-1
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java
.../format/datetime/standard/DateTimeFormatterRegistrar.java
+5
-1
spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java
...framework/format/datetime/standard/DurationFormatter.java
+47
-0
spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java
...ngframework/format/datetime/standard/PeriodFormatter.java
+48
-0
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
...ork/format/datetime/standard/DateTimeFormattingTests.java
+40
-0
未找到文件。
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java
浏览文件 @
d5ca5b74
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -85,6 +85,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
LocalDateTimeToLocalDateConverter
implements
Converter
<
LocalDateTime
,
LocalDate
>
{
...
...
@@ -94,6 +95,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
LocalDateTimeToLocalTimeConverter
implements
Converter
<
LocalDateTime
,
LocalTime
>
{
...
...
@@ -103,6 +105,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
ZonedDateTimeToLocalDateConverter
implements
Converter
<
ZonedDateTime
,
LocalDate
>
{
...
...
@@ -112,6 +115,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
ZonedDateTimeToLocalTimeConverter
implements
Converter
<
ZonedDateTime
,
LocalTime
>
{
...
...
@@ -121,6 +125,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
ZonedDateTimeToLocalDateTimeConverter
implements
Converter
<
ZonedDateTime
,
LocalDateTime
>
{
...
...
@@ -139,6 +144,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
ZonedDateTimeToInstantConverter
implements
Converter
<
ZonedDateTime
,
Instant
>
{
...
...
@@ -149,6 +155,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
OffsetDateTimeToLocalDateConverter
implements
Converter
<
OffsetDateTime
,
LocalDate
>
{
...
...
@@ -158,6 +165,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
OffsetDateTimeToLocalTimeConverter
implements
Converter
<
OffsetDateTime
,
LocalTime
>
{
...
...
@@ -167,6 +175,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
OffsetDateTimeToLocalDateTimeConverter
implements
Converter
<
OffsetDateTime
,
LocalDateTime
>
{
...
...
@@ -176,6 +185,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
OffsetDateTimeToZonedDateTimeConverter
implements
Converter
<
OffsetDateTime
,
ZonedDateTime
>
{
...
...
@@ -185,6 +195,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
OffsetDateTimeToInstantConverter
implements
Converter
<
OffsetDateTime
,
Instant
>
{
...
...
@@ -194,6 +205,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToZonedDateTimeConverter
implements
Converter
<
Calendar
,
ZonedDateTime
>
{
...
...
@@ -203,6 +215,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToOffsetDateTimeConverter
implements
Converter
<
Calendar
,
OffsetDateTime
>
{
...
...
@@ -212,6 +225,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToLocalDateConverter
implements
Converter
<
Calendar
,
LocalDate
>
{
...
...
@@ -221,6 +235,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToLocalTimeConverter
implements
Converter
<
Calendar
,
LocalTime
>
{
...
...
@@ -230,6 +245,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToLocalDateTimeConverter
implements
Converter
<
Calendar
,
LocalDateTime
>
{
...
...
@@ -239,6 +255,7 @@ final class DateTimeConverters {
}
}
@UsesJava8
private
static
class
CalendarToInstantConverter
implements
Converter
<
Calendar
,
Instant
>
{
...
...
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java
浏览文件 @
d5ca5b74
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,12 +16,14 @@
package
org.springframework.format.datetime.standard
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.OffsetDateTime
;
import
java.time.OffsetTime
;
import
java.time.Period
;
import
java.time.ZonedDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.FormatStyle
;
...
...
@@ -182,6 +184,8 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar {
new
TemporalAccessorParser
(
OffsetTime
.
class
,
timeFormatter
));
registry
.
addFormatterForFieldType
(
Instant
.
class
,
new
InstantFormatter
());
registry
.
addFormatterForFieldType
(
Period
.
class
,
new
PeriodFormatter
());
registry
.
addFormatterForFieldType
(
Duration
.
class
,
new
DurationFormatter
());
registry
.
addFormatterForFieldAnnotation
(
new
Jsr310DateTimeFormatAnnotationFormatterFactory
());
}
...
...
spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java
0 → 100644
浏览文件 @
d5ca5b74
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.format.datetime.standard
;
import
java.text.ParseException
;
import
java.time.Duration
;
import
java.util.Locale
;
import
org.springframework.format.Formatter
;
import
org.springframework.lang.UsesJava8
;
/**
* {@link Formatter} implementation for a JSR-310 {@link Duration},
* following JSR-310's parsing rules for a Duration.
*
* @author Juergen Hoeller
* @since 4.2.4
* @see Duration#parse
*/
@UsesJava8
public
class
DurationFormatter
implements
Formatter
<
Duration
>
{
@Override
public
Duration
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
Duration
.
parse
(
text
);
}
@Override
public
String
print
(
Duration
object
,
Locale
locale
)
{
return
object
.
toString
();
}
}
spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java
0 → 100644
浏览文件 @
d5ca5b74
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.format.datetime.standard
;
import
java.text.ParseException
;
import
java.time.Instant
;
import
java.time.Period
;
import
java.util.Locale
;
import
org.springframework.format.Formatter
;
import
org.springframework.lang.UsesJava8
;
/**
* {@link Formatter} implementation for a JSR-310 {@link Period},
* following JSR-310's parsing rules for a Period.
*
* @author Juergen Hoeller
* @since 4.2.4
* @see Period#parse
*/
@UsesJava8
public
class
PeriodFormatter
implements
Formatter
<
Period
>
{
@Override
public
Period
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
Period
.
parse
(
text
);
}
@Override
public
String
print
(
Period
object
,
Locale
locale
)
{
return
object
.
toString
();
}
}
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
浏览文件 @
d5ca5b74
...
...
@@ -16,10 +16,12 @@
package
org.springframework.format.datetime.standard
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.Period
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.FormatStyle
;
...
...
@@ -334,6 +336,24 @@ public class DateTimeFormattingTests {
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"instant"
).
toString
().
startsWith
(
"2009-10-31"
));
}
@Test
public
void
testBindPeriod
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"period"
,
"P6Y3M1D"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"period"
).
toString
().
equals
(
"P6Y3M1D"
));
}
@Test
public
void
testBindDuration
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"duration"
,
"PT8H6M12.345S"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"duration"
).
toString
().
equals
(
"PT8H6M12.345S"
));
}
public
static
class
DateTimeBean
{
...
...
@@ -366,6 +386,10 @@ public class DateTimeFormattingTests {
private
Instant
instant
;
private
Period
period
;
private
Duration
duration
;
private
final
List
<
DateTimeBean
>
children
=
new
ArrayList
<
DateTimeBean
>();
public
LocalDate
getLocalDate
()
{
...
...
@@ -456,6 +480,22 @@ public class DateTimeFormattingTests {
this
.
instant
=
instant
;
}
public
Period
getPeriod
()
{
return
period
;
}
public
void
setPeriod
(
Period
period
)
{
this
.
period
=
period
;
}
public
Duration
getDuration
()
{
return
duration
;
}
public
void
setDuration
(
Duration
duration
)
{
this
.
duration
=
duration
;
}
public
List
<
DateTimeBean
>
getChildren
()
{
return
children
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录