JSTL标签fmt:formatDate格式化日期出错|全球新要闻

来源: 博客园
现象&背景:

异常:


(资料图片仅供参考)

"org.apache.jasper.JasperException: 在 [115] 行处理 [/WEB-INF/jsp/modules/receivedyaccnotify/receive_dy_acc_notify.jsp] 时发生异常" 代码:<%@ taglib prefix= "fmt" uri= "http://java.sun.com/jsp/jstl/fmt" %>

原因:

关于这个异常的详细说明,可以参考【https://blog.csdn.net/lzf_hlh/article/details/81737222】,这次的异常发生的原因是类型转换导致的:即因为${record.transTime}取出来是String类型,无法转换为Date类型。

详解:

从代码中进入【fmt:formatDate】的JSTL源代码中可以发现以下描述(红色字体):

【Date and/or time to be formatted.】:需要转换的value得是Date对象类型

【Name of the exported scoped variable which stores the formatted result as a String.】:返回的值是String类型

1  2          3             Formats a date and/or time using the supplied styles and pattern 4          5         formatDate 6         org.apache.taglibs.standard.tag.rt.fmt.FormatDateTag 7         empty 8          9             10                 Date and/or time to be formatted.11             12             value13             true14             true15         16         17             18                 Specifies whether the time, the date, or both19                 the time and date components of the given20                 date are to be formatted.21             22             type23             false24             true25         26         27             28                 Predefined formatting style for dates. Follows29                 the semantics defined in class30                 java.text.DateFormat. Applied only31                 when formatting a date or both a date and32                 time (i.e. if type is missing or is equal to33                 "date" or "both"); ignored otherwise.34             35             dateStyle36             false37             true38         39         40             41                 Predefined formatting style for times. Follows42                 the semantics defined in class43                 java.text.DateFormat. Applied only44                 when formatting a time or both a date and45                 time (i.e. if type is equal to "time" or "both");46                 ignored otherwise.47             48             timeStyle49             false50             true51         52         53             54                 Custom formatting style for dates and times.55             56             pattern57             false58             true59         60         61             62                 Time zone in which to represent the formatted63                 time.64             65             timeZone66             false67             true68         69         70             71                 Name of the exported scoped variable which72                 stores the formatted result as a String.73             74             var75             false76             false77         78         79             80                 Scope of var.81             82             scope83             false84             false85         86     

因为从数据库读取的字段为字符串格式的"20230321165837",通过【fmt:formatDate】转换的时候以致发生异常。

所以在调用之前将所需要转换的项目变成Date类型,那么可以通过【fmt:parseDate】进行转换解析,参考代码如下:

1 2       //yyyyMMddHHmmss见补充说明※1.3                        //yyyy-MM-dd HH:mm:ss见补充说明※3.  type="date"见补充说明※2.4 

补充说明:

※1.关于【yyyyyMMddHHmmss】:与转换的日期字符串的格式一致,否则可能回报【value attribute can not be parsed: "20230321165837"】错误。

示例1:

日期字符串:"20230321165837:

转换格式:"yyyyyMMddHHmmss"

示例2:

日期字符串:"2023/03/21 16:58:37"

转换格式:"yyyy/MM/dd HH:mm:ss"

示例3:

日期字符串:"2021-06-10T22:04:34"

转换格式:"yyyy-MM-dd"T"HH:mm:ss"

示例4:

日期字符串:"2021-06-10T22:04:34+08:00"

转换格式:"yyyy-MM-dd"T"HH:mm:ssXXX"

※2.关于【type="date"】:type有三个值【date, time, both】

type="date":只显示日期部分

type="time":只显示时间部分,精确到秒

type="both":显示日期+时间,精确到秒

如果pattern="yyyy-MM-dd HH:mm:ss"已经设置格式了,可以不要type属性,如果没有设置pattern属性,常规的日期+时间格式的显示,用type="both"即可(也可直接设置pattern属性),

但是对于非常规格式的日期(如示例4),需要指定pattern="yyyy-MM-dd"T"HH:mm:ssXXX",此时可以不要type属性了。

▲注意:pattern的优先级高于type,即使设置了type,只要pattern被设置,最终的显示格式会以pattern显示。

※3.关于【yyyy-MM-dd HH:mm:ss】:这个可以是任意

※4.关于【fmt:parseDate】的入值与出值的类型说明:

【Date string to be parsed.】:需要转换的value得是日期字符串

【Name of the exported scoped variable in which the parsing result (of type java.util.Date) is stored.】:返回的值是java.util.Date类型

1   2           3             Parses the string representation of a date and/or time  4           5         parseDate  6         org.apache.taglibs.standard.tag.rt.fmt.ParseDateTag  7         JSP  8           9              10                 Date stringto be parsed. 11              12             value 13             false 14             true 15          16          17              18                 Specifies whether the date string in the 19                 value attribute is supposed to contain a 20                 time, a date, or both. 21              22             type 23             false 24             true 25          26          27              28                 Predefined formatting style for days 29                 which determines how the date 30                 component of the date string is to be 31                 parsed. Applied only when formatting a 32                 date or both a date and time (i.e. if type 33                 is missing or is equal to "date" or "both"); 34                 ignored otherwise. 35              36             dateStyle 37             false 38             true 39          40          41              42                 Predefined formatting styles for times 43                 which determines how the time 44                 component in the date string is to be 45                 parsed. Applied only when formatting a 46                 time or both a date and time (i.e. if type 47                 is equal to "time" or "both"); ignored 48                 otherwise. 49              50             timeStyle 51             false 52             true 53          54          55              56                 Custom formatting pattern which 57                 determines how the date string is to be 58                 parsed. 59              60             pattern 61             false 62             true 63          64          65              66                 Time zone in which to interpret any time 67                 information in the date string. 68              69             timeZone 70             false 71             true 72          73          74              75                 Locale whose predefined formatting styles 76                 for dates and times are to be used during 77                 the parse operation, or to which the 78                 pattern specified via the pattern 79                 attribute (if present) is applied. 80              81             parseLocale 82             false 83             true 84          85          86              87                 Name of the exported scoped variable in 88                 which the parsing result (of type 89                 java.util.Date) is stored. 90              91             var 92             false 93             false 94          95          96              97                 Scope of var. 98              99             scope100             false101             false102         103     
相关新闻