2019独角兽企业重金招聘Python工程师标准>>>
两种用法示例:
@GetMapping("/date")
public String datest(@DateTimeFormat(iso=ISO.DATE) Date date){
System.out.println(date);
return "lalalal";
}
@GetMapping("/date")
public String datest(@DateTimeFormat(pattern = "yyyy/MM/dd") Date date){
System.out.println(date);
return "lalalal";
}
使用 @DateTimeFormat 注解有两种做法,如上面代码所示。
示例说明:
第一种是按照ISO国际标准的日期时间格式解析参数中的日期时间的,具体格式为此枚举变量:
/**
* Common ISO date time format patterns.
*/
enum ISO {
/**
* The most common ISO Date Format {@code yyyy-MM-dd},
* e.g. "2000-10-31".
*/
DATE,
/**
* The most common ISO Time Format {@code HH:mm:ss.SSSZ},
* e.g. "01:30:00.000-05:00".
*/
TIME,
/**
* The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ},
* e.g. "2000-10-31T01:30:00.000-05:00".
* This is the default if no annotation value is specified.
*/
DATE_TIME,
/**
* Indicates that no ISO-based format pattern should be applied.
*/
NONE
}
第二种是按照自定义的模式来解析时间,模式字符串的定义与遵循以下规则(摘自jdk 1.8 SimpleDateFormat):
-
The following pattern letters are defined (all other characters from
'A'
to'Z'
and from'a'
to'z'
are reserved):Letter Date or Time Component Presentation Examples G
Era designator Text AD
y
Year Year 1996
;96
Y
Week year Year 2009
;09
M
Month in year (context sensitive) Month July
;Jul
;07
L
Month in year (standalone form) Month July
;Jul
;07
w
Week in year Number 27
W
Week in month Number 2
D
Day in year Number 189
d
Day in month Number 10
F
Day of week in month Number 2
E
Day name in week Text Tuesday
;Tue
u
Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a
Am/pm marker Text PM
H
Hour in day (0-23) Number 0
k
Hour in day (1-24) Number 24
K
Hour in am/pm (0-11) Number 0
h
Hour in am/pm (1-12) Number 12
m
Minute in hour Number 30
s
Second in minute Number 55
S
Millisecond Number 978
z
Time zone General time zone Pacific Standard Time
;PST
;GMT-08:00
Z
Time zone RFC 822 time zone -0800
X
Time zone ISO 8601 time zone -08
;-0800
;-08:00
- The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone.
Date and Time Pattern Result "yyyy.MM.dd G 'at' HH:mm:ss z"
2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"
Wed, Jul 4, '01
"h:mm a"
12:08 PM
"hh 'o''clock' a, zzzz"
12 o'clock PM, Pacific Daylight Time
"K:mm a, z"
0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"
02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"
Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"
010704120856-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
2001-07-04T12:08:56.235-0700
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
2001-07-04T12:08:56.235-07:00
"YYYY-'W'ww-u"
2001-W27-3
其他补充:
@ExceptionHandler 异常处理:
@ExceptionHandler(Exception.class)
public String handle(Exception exception){
if (exception instanceof MethodArgumentTypeMismatchException) {
return "传入参数格式不正确或参数解析异常!";
}
return exception.getMessage();
}
如上所示,利用此注解@ExceptionHandler就可以实现对异常的处理。
若此示例代码段放在@Controller类中,就只可以处理它所在类被请求过程中发生的异常。如果需要处理多个@Controller类被请求过程中发生的异常,就需要将此代码段放在单独的一个类中,并且这个类需要加上@ControllerAdvice(或@RestControllerAdvice)注解。