Filter之编码格式(日期限制)

package com.tools;

/* * 转码(过期时间限制)

 * @author hh

 * @2010-09-16

 */

import java.io.IOException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")

 public class filter implements Filter {

       private String CEncoding="";

       public void destroy() {

       }

       @SuppressWarnings("static-access")

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2) throws IOException, ServletException {

             Calendar c = Calendar.getInstance();

             HttpServletRequest req = (HttpServletRequest)request;

             HttpServletResponse res = (HttpServletResponse)response;

             req.setCharacterEncoding(CEncoding);

             res.setCharacterEncoding(CEncoding);

             String nowDate="2011-03-25";//开始时间

             long flage = betweenTwoDay(nowDate, new SimpleDateFormat("yyyy-MM-dd").format(c.getInstance().getTime()));

            if(flage>=0&&flage<=30){

                 arg2.doFilter(req, res);

            }else{

                 req.getRequestDispatcher("/TimeOut.jsp").forward(req,res);//提示界面

           }

     }

     private long betweenTwoDay(String first, String second) {

               Date fdate = getStringToDate(first);

               Date sdate = getStringToDate(second);

               long i = ((sdate.getTime() - fdate.getTime()) / (24 * 60 * 60 * 1000));

               return i;

    }

   private String format = "";

   private Date getStringToDate(String date) {

          Date d = null;

          try {

                  SimpleDateFormat dateFormat = null;

                  if (date.length() == 10) {

                       dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                       format = "yyyy-MM-dd";

                      dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));

                 } else if (date.length() == 16) {

                      dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

                      format = "yyyy-MM-dd HH:mm";

                     dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));

                 } else if (date.length() == 19) {

                     dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                     format = "yyyy-MM-dd HH:mm:ss";

                    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));

                 }

                d = dateFormat.parse(date);

             } catch (ParseException e) {

                   e.printStackTrace();

             }

         return d;

    }

    public void init(FilterConfig arg0) throws ServletException {

          //读取web.xml文件设置编码格式

          CEncoding = arg0.getInitParameter("encoding");

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值