时区tag插件

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值