package com.company.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
@SuppressWarnings("serial")
public class DateFormatTag extends TagSupport{
/**
* 后台返回的自1970-1-1 00:00:00-至今的毫秒数
*/
private long logNum;
/**
* 客户端时区
*/
private int clientZone;
@SuppressWarnings("static-access")
@Override
public int doStartTag() throws JspException {
JspWriter out = this.pageContext.getOut();
//将临时区的时间转换
Calendar calendar=Calendar.getInstance();
//设置时间
calendar.setTimeInMillis(logNum);
//获取客户端时区与服务器时区之间的差值
calendar.add(calendar.HOUR_OF_DAY, clientZone);
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr=dateFormat.format(calendar.getTime());
try {
out.println(dateStr);
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_PAGE;
}
public long getLogNum() {
return logNum;
}
public void setLogNum(long logNum) {
this.logNum = logNum;
}
public int getClientZone() {
return clientZone;
}
public void setClientZone(int clientZone) {
this.clientZone = clientZone;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.TimeZone"%>
<%@page import="java.util.Date"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<%
Date timeZone=new Date();
%>
</head>
<body>
<script type="text/javascript">
var d = new Date();
document.write(d.getTimezoneOffset()/60);
</script>
<br/>
<%=timeZone.getTimezoneOffset()%>
<!-- Asia/Shanghai -->
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>page</short-name>
<tag>
<name>DateFormatTag</name>
<tag-class>com.company.tag.DateFormatTag</tag-class>
<!--这里是自定义标签类的完整类名:我这里是DateFormatTag-->
<body-content>empty</body-content>
<attribute>
<name>logNum</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>clientZone</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>