Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
ef2e1691
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,发现更多精彩内容 >>
提交
ef2e1691
编写于
1月 30, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Formatting support for java.time.Year and java.time.Month
Issue: SPR-16437
上级
89d2bd95
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
162 addition
and
19 deletion
+162
-19
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/MonthFormatter.java
...ingframework/format/datetime/standard/MonthFormatter.java
+45
-0
spring-context/src/main/java/org/springframework/format/datetime/standard/YearFormatter.java
...ringframework/format/datetime/standard/YearFormatter.java
+45
-0
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
...ork/format/datetime/standard/DateTimeFormattingTests.java
+67
-18
未找到文件。
spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java
浏览文件 @
ef2e1691
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -21,10 +21,12 @@ import java.time.Instant;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.Month
;
import
java.time.MonthDay
;
import
java.time.OffsetDateTime
;
import
java.time.OffsetTime
;
import
java.time.Period
;
import
java.time.Year
;
import
java.time.YearMonth
;
import
java.time.ZonedDateTime
;
import
java.time.format.DateTimeFormatter
;
...
...
@@ -190,6 +192,8 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar {
registry
.
addFormatterForFieldType
(
Instant
.
class
,
new
InstantFormatter
());
registry
.
addFormatterForFieldType
(
Period
.
class
,
new
PeriodFormatter
());
registry
.
addFormatterForFieldType
(
Duration
.
class
,
new
DurationFormatter
());
registry
.
addFormatterForFieldType
(
Year
.
class
,
new
YearFormatter
());
registry
.
addFormatterForFieldType
(
Month
.
class
,
new
MonthFormatter
());
registry
.
addFormatterForFieldType
(
YearMonth
.
class
,
new
YearMonthFormatter
());
registry
.
addFormatterForFieldType
(
MonthDay
.
class
,
new
MonthDayFormatter
());
...
...
spring-context/src/main/java/org/springframework/format/datetime/standard/MonthFormatter.java
0 → 100644
浏览文件 @
ef2e1691
/*
* Copyright 2002-2018 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.Month
;
import
java.util.Locale
;
import
org.springframework.format.Formatter
;
/**
* {@link Formatter} implementation for a JSR-310 {@link Month},
* resolving a given String against the Month enum values (ignoring case).
*
* @author Juergen Hoeller
* @since 5.0.4
* @see Month#valueOf
*/
class
MonthFormatter
implements
Formatter
<
Month
>
{
@Override
public
Month
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
Month
.
valueOf
(
text
.
toUpperCase
());
}
@Override
public
String
print
(
Month
object
,
Locale
locale
)
{
return
object
.
toString
();
}
}
spring-context/src/main/java/org/springframework/format/datetime/standard/YearFormatter.java
0 → 100644
浏览文件 @
ef2e1691
/*
* Copyright 2002-2018 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.Year
;
import
java.util.Locale
;
import
org.springframework.format.Formatter
;
/**
* {@link Formatter} implementation for a JSR-310 {@link Year},
* following JSR-310's parsing rules for a Year.
*
* @author Juergen Hoeller
* @since 5.0.4
* @see Year#parse
*/
class
YearFormatter
implements
Formatter
<
Year
>
{
@Override
public
Year
parse
(
String
text
,
Locale
locale
)
throws
ParseException
{
return
Year
.
parse
(
text
);
}
@Override
public
String
print
(
Year
object
,
Locale
locale
)
{
return
object
.
toString
();
}
}
spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java
浏览文件 @
ef2e1691
...
...
@@ -21,8 +21,10 @@ import java.time.Instant;
import
java.time.LocalDate
;
import
java.time.LocalDateTime
;
import
java.time.LocalTime
;
import
java.time.Month
;
import
java.time.MonthDay
;
import
java.time.Period
;
import
java.time.Year
;
import
java.time.YearMonth
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
...
...
@@ -60,12 +62,12 @@ public class DateTimeFormattingTests {
@Before
public
void
set
U
p
()
{
public
void
set
u
p
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
set
U
p
(
registrar
);
set
u
p
(
registrar
);
}
private
void
set
U
p
(
DateTimeFormatterRegistrar
registrar
)
{
private
void
set
u
p
(
DateTimeFormatterRegistrar
registrar
)
{
conversionService
=
new
FormattingConversionService
();
DefaultConversionService
.
addDefaultConverters
(
conversionService
);
registrar
.
registerFormatters
(
conversionService
);
...
...
@@ -82,7 +84,7 @@ public class DateTimeFormattingTests {
}
@After
public
void
tearDown
()
{
public
void
cleanup
()
{
LocaleContextHolder
.
setLocale
(
null
);
DateTimeContextHolder
.
setDateTimeContext
(
null
);
}
...
...
@@ -98,10 +100,10 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalDateWithSpecificStyle
()
throws
Exception
{
public
void
testBindLocalDateWithSpecificStyle
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
registrar
.
setDateStyle
(
FormatStyle
.
LONG
);
set
U
p
(
registrar
);
set
u
p
(
registrar
);
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localDate"
,
"October 31, 2009"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -110,10 +112,10 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalDateWithSpecificFormatter
()
throws
Exception
{
public
void
testBindLocalDateWithSpecificFormatter
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
registrar
.
setDateFormatter
(
DateTimeFormatter
.
ofPattern
(
"yyyyMMdd"
));
set
U
p
(
registrar
);
set
u
p
(
registrar
);
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localDate"
,
"20091031"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -177,7 +179,7 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalDateFromJavaUtilCalendar
()
throws
Exception
{
public
void
testBindLocalDateFromJavaUtilCalendar
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localDate"
,
new
GregorianCalendar
(
2009
,
9
,
31
,
0
,
0
));
binder
.
bind
(
propertyValues
);
...
...
@@ -195,10 +197,10 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalTimeWithSpecificStyle
()
throws
Exception
{
public
void
testBindLocalTimeWithSpecificStyle
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
registrar
.
setTimeStyle
(
FormatStyle
.
MEDIUM
);
set
U
p
(
registrar
);
set
u
p
(
registrar
);
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localTime"
,
"12:00:00 PM"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -207,10 +209,10 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalTimeWithSpecificFormatter
()
throws
Exception
{
public
void
testBindLocalTimeWithSpecificFormatter
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
registrar
.
setTimeFormatter
(
DateTimeFormatter
.
ofPattern
(
"HHmmss"
));
set
U
p
(
registrar
);
set
u
p
(
registrar
);
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localTime"
,
"130000"
);
binder
.
bind
(
propertyValues
);
...
...
@@ -228,7 +230,7 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalTimeFromJavaUtilCalendar
()
throws
Exception
{
public
void
testBindLocalTimeFromJavaUtilCalendar
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localTime"
,
new
GregorianCalendar
(
1970
,
0
,
0
,
12
,
0
));
binder
.
bind
(
propertyValues
);
...
...
@@ -259,7 +261,7 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindLocalDateTimeFromJavaUtilCalendar
()
throws
Exception
{
public
void
testBindLocalDateTimeFromJavaUtilCalendar
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localDateTime"
,
new
GregorianCalendar
(
2009
,
9
,
31
,
12
,
0
));
binder
.
bind
(
propertyValues
);
...
...
@@ -270,10 +272,10 @@ public class DateTimeFormattingTests {
}
@Test
public
void
testBindDateTimeWithSpecificStyle
()
throws
Exception
{
public
void
testBindDateTimeWithSpecificStyle
()
{
DateTimeFormatterRegistrar
registrar
=
new
DateTimeFormatterRegistrar
();
registrar
.
setDateTimeStyle
(
FormatStyle
.
MEDIUM
);
set
U
p
(
registrar
);
set
u
p
(
registrar
);
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"localDateTime"
,
LocalDateTime
.
of
(
2009
,
10
,
31
,
12
,
0
));
binder
.
bind
(
propertyValues
);
...
...
@@ -356,7 +358,7 @@ public class DateTimeFormattingTests {
@Test
@SuppressWarnings
(
"deprecation"
)
public
void
testBindInstantFromJavaUtilDate
()
throws
Exception
{
public
void
testBindInstantFromJavaUtilDate
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"instant"
,
new
Date
(
109
,
9
,
31
,
12
,
0
));
binder
.
bind
(
propertyValues
);
...
...
@@ -382,6 +384,33 @@ public class DateTimeFormattingTests {
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"duration"
).
toString
().
equals
(
"PT8H6M12.345S"
));
}
@Test
public
void
testBindYear
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"year"
,
"2007"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"year"
).
toString
().
equals
(
"2007"
));
}
@Test
public
void
testBindMonth
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"month"
,
"JULY"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"month"
).
toString
().
equals
(
"JULY"
));
}
@Test
public
void
testBindMonthInAnyCase
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
propertyValues
.
add
(
"month"
,
"July"
);
binder
.
bind
(
propertyValues
);
assertEquals
(
0
,
binder
.
getBindingResult
().
getErrorCount
());
assertTrue
(
binder
.
getBindingResult
().
getFieldValue
(
"month"
).
toString
().
equals
(
"JULY"
));
}
@Test
public
void
testBindYearMonth
()
{
MutablePropertyValues
propertyValues
=
new
MutablePropertyValues
();
...
...
@@ -436,6 +465,10 @@ public class DateTimeFormattingTests {
private
Duration
duration
;
private
Year
year
;
private
Month
month
;
private
YearMonth
yearMonth
;
private
MonthDay
monthDay
;
...
...
@@ -546,6 +579,22 @@ public class DateTimeFormattingTests {
this
.
duration
=
duration
;
}
public
Year
getYear
()
{
return
year
;
}
public
void
setYear
(
Year
year
)
{
this
.
year
=
year
;
}
public
Month
getMonth
()
{
return
month
;
}
public
void
setMonth
(
Month
month
)
{
this
.
month
=
month
;
}
public
YearMonth
getYearMonth
()
{
return
yearMonth
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录