首页 > 代码库 > [Java] JSTL格式化时间计算时差
[Java] JSTL格式化时间计算时差
引入JSLT标签:
<%@ taglib uri=
"http://java.sun.com/jsp/jstl/core"
prefix=
"c"
%>
<%@ taglib uri=
"http://java.sun.com/jsp/jstl/fmt"
prefix=
"fmt"
%>
字符串someDate:
<c:set var=
"someDateStr"
value=http://www.mamicode.com/
"2014-05-08 18:02:01"
/>
转换Date对象:
<fmt:parseDate var=
"someDate"
value=http://www.mamicode.com/
"${someDateStr}"
pattern=
"yyyy-MM-dd HH:mm:ss"
/>
parseDate:${someDateStr} >> ${someDate}
页面输出:
parseDate:2014-05-08 18:02:01 >> Thu May 08 18:02:01 CST 2014
当前时间对象:
<jsp:useBean id=
"nowDate"
class
=
"java.util.Date"
/>
格式化为字符串:
<fmt:formatDate var=
"nowStr"
value=http://www.mamicode.com/
"${nowDate}"
pattern=
"yyyy/MM/dd HH:mm:ss"
/>
formatDate:${nowDate} >> ${nowStr}
页面输出:
formatDate:Thu May 08 18:49:57 CST 2014 >> 2014/05/08 18:49:57
计算时间差:
<c:set var=
"interval"
value=http://www.mamicode.com/
"${nowDate.time - someDate.time}"
/>
时间间隔(毫秒):${interval}
页面输出:
时间间隔(毫秒):2876802
转换为分钟:
<fmt:formatNumber value=http://www.mamicode.com/
"${interval/1000/60}"
pattern=
"#0.0"
/>
页面输出: 47.9