SpringMVC,Mybatis,FreeMarker连接mycat示例(三)

命运的转折是从当下的这一秒开始的,而最可怕的一种局面是,怀念过去,幻想未来,虚度现在。

工具类包结构:

这里写图片描述

此项目没有用到几个,就不全部贴出来了

AesUtils.java代码:

package com.base.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import com.base.common.Constant;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * AES加密工具类 
 * 可实现字符串加密和解密,采用AES加密算法
 */
public class AesUtils {
    protected static Log log = LogFactory.getLog(AesUtils.class);

    /**
     * Url 加密
     * 
     * @param argStr
     *            参数字符串
     * @return
     */
    public static String encode(String argStr) {
        String result = null;
        try {
            byte[] byteRe = enCrypt(argStr, Constant.URL_KEY);
            result = parseByte2HexStr(byteRe);
        } catch (Exception e) {
            log.error("Url加密失败!", e);
        }
        return result;
    }

    /**
     * Url 解密
     * 
     * @param encrytStr
     *            加密的参数字符串
     * @return
     */
    public static String decode(String encrytStr) {
        String result = null;
        try {
            byte[] encrytByte = parseHexStr2Byte(encrytStr);
            result = deCrypt(encrytByte, Constant.URL_KEY);
        } catch (Exception e) {
            log.error("Url解密失败!", e);
        }
        return result;
    }

    /**
     * 加密函数
     * 
     * @param content
     *            加密的内容
     * @param strKey
     *            密钥
     * @return 返回二进制字符数组
     * @throws Exception
     */
    public static byte[] enCrypt(String content, String strKey)
            throws Exception {
        byte[] cByte = null;
        try {
            KeyGenerator keygen;
            SecretKey desKey;
            Cipher c;
            String str = content;
            keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(strKey.getBytes()));
            desKey = keygen.generateKey();
            c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, desKey);
            cByte = c.doFinal(str.getBytes("UTF-8"));
        } catch (Exception e) {
            log.error("AES加密操作失败!", e);
        }
        return cByte;
    }

    /**
     * 解密函数
     * 
     * @param src
     *            加密过的二进制字符数组
     * @param strKey
     *            密钥
     * @return
     * @throws Exception
     */
    public static String deCrypt(byte[] src, String strKey) throws Exception {
        byte[] cByte = null;
        try {
            KeyGenerator keygen;
            SecretKey desKey;
            Cipher c;
            keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(strKey.getBytes()));
            desKey = keygen.generateKey();
            c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, desKey);
            cByte = c.doFinal(src);
        } catch (Exception e) {
            log.error("AES解密失败!", e);
        }
        return new String(cByte, "UTF-8");
    }

    /**
     * 将2进制转化成16进制
     * 
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        try {
            for (int i = 0; i < buf.length; i++) {
                String hex = Integer.toHexString(buf[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex.toUpperCase());
            }
        } catch (Exception e) {
            log.error("将2进制转化成16进制失败!", e);
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     * 
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(encode(35+""));
    }
}

DateTimeUtils.java代码:

package com.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 日期和时间的常用类<br>
 * 方法都是static的,可通过类名调用<br>
 */
public class DateTimeUtils {

    private static Log log = LogFactory.getLog(DateTimeUtils.class);
    private static final String DATEPATTERN = "yyyy-MM-dd";
    private static final String TIMEPATTERN = "HH:mm";
    private static final String DATETIMEPATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATESTR = "yyyyMMdd";
    private static final String TIMEZONEPATTERN = "yyyyMMddHHmmss";

    // 不允许实例化该类
    private DateTimeUtils(){
    }

    /**
     * 获得系统当前日期时间
     *
     * @return 格式为 yyyy-MM-dd HH:mm:ss
     */
    public static String getNowDateTime() {
        return new SimpleDateFormat(DATETIMEPATTERN).format(new Date());
    }

    /**
     * 获得系统当前日期
     *
     * @return 格式为 yyyy-MM-dd
     */
    public static String getDate() {
        return (new SimpleDateFormat(DATEPATTERN)).format(new Date());
    }

    /**
     * 获得指定时间的标准返回格式
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 格式为 yyyy-MM-dd
     */
    public static String getDate(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat(DATEPATTERN)).format(calendar.getTime());
    }

    /**
     * 得到"yyyy年M月d日"格式的日期
     *
     * @return "yyyy年M月d日"格式的日期
     */
    public static String getChineseDate() {
        return (new SimpleDateFormat("yyyy\u5E74M\u6708d\u65E5"))
                .format(new Date());
    }

    /**
     * 得到当前的时间,格式为"HH:mm:ss"
     *
     * @return 当前时间,格式为"HH:mm:ss"
     */
    public static String getTime() {
        return (new SimpleDateFormat("HH:mm:ss")).format(new Date());
    }

    /**
     * 得到当前时间
     *
     * @return 数组格式的当前时间,array[0]为小时,array[1]为分钟 String[]
     */
    public static String[] getTimeForHourMinute() {
        String time[] = new String[2];
        String timeStr = (new SimpleDateFormat("HH:mm:ss")).format(new Date());
        String temp[] = timeStr.split(":");
        time[0] = temp[0];
        time[1] = temp[1];
        return time;
    }

    /**
     * 得到当前日期的星期
     *
     * @return 当前日期的星期 String
     */
    public static String getWeekday() {
        return (new SimpleDateFormat("E")).format(new Date());
    }

    /**
     * 获取时间字符串
     * @return 格式为 yyyyMMdd
     */
    public static String getDateStr(){
        return (new SimpleDateFormat(DATESTR)).format(new Date());
    }

    /**
     * 将当前的时间增加指定的小时
     *
     * @param hour
     * @return
     */
    public static String getDateTimeByHour(int hour) {
        String dateTime = null;
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, hour);// 增加指定的小时
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIMEPATTERN);
        dateTime = sdf.format(c.getTime());
        return dateTime;
    }

    /**
     * 得到指定日期的星期
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 日期 String
     */
    public static String getWeekday(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat("E")).format(calendar.getTime());
    }

    /**
     * 得到指定日期前/后offset天的日期
     *
     * @param dateString 以"yyyy-MM-dd"格式指定的日期
     * @param offset     偏移量
     * @return String
     */
    public static String getDate(String dateString, int offset) {
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
            calendar.setTime(date);
        } catch (Exception e) {
            return "";
        }
        calendar.add(6, -1 * offset);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    /**
     * 得到当前日期的年份和月份
     *
     * @return 以"yyyy-MM"表示的年份和月份 String
     */
    public static String getYearAndMonth() {
        return (new SimpleDateFormat("yyyy-MM")).format(new Date());
    }

    /**
     * 得到指定日期的年份和月份
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 年份和月份 String
     */
    public static String getYearAndMonth(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat("yyyy-MM")).format(calendar.getTime());
    }

    /**
     * 得到当前日期是一年中的第几天
     *
     * @return String
     */
    public static String getDateInYear() {
        return (new SimpleDateFormat("DDD")).format(new Date());
    }

    /**
     * 得到指定日期是一年中的第几天
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 指定日期在一年中的第几天 String
     */
    public static String getDateInYear(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat("DDD")).format(calendar.getTime());
    }

    /**
     * 得到当前日期是一年中的第几个星期
     *
     * @return String
     */
    public static String getWeekInYear() {
        return (new SimpleDateFormat("ww")).format(new Date());
    }

    /**
     * 得到指定日期是一年中的第几个星期
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 指定日期是一年中的第几个星期 String
     */
    public static String getWeekInYear(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat("ww")).format(calendar.getTime());
    }

    /**
     * 得到指定日期是当前月的第几个星期
     *
     * @return String
     */
    public static String getWeekInMonth() {
        return (new SimpleDateFormat("WW")).format(new Date());
    }

    /**
     * 得到指定日期是所在月份的第几个星期
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @return 指定日期是所在月份的第几个星期 String
     */
    public static String getWeekInMonth(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return (new SimpleDateFormat("WW")).format(calendar.getTime());
    }

    /**
     * 得到当前日期前beforeNum天的日期
     *
     * @param beforeNum
     *            提前量
     * @return String
     */
    public static String getDateByBefore(int beforeNum) {
        Calendar now = Calendar.getInstance();
        now.add(6, -1 * beforeNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
    }

    /**
     * 得到指定日期前beforeNum天的日期
     *
     * @param year
     *            指定的年份
     * @param month
     *            指定的月份
     * @param day
     *            指定的日期
     * @param beforeNum
     *            提前量
     * @return String
     */
    public static String getDateByBefore(int year, int month, int day,
                                         int beforeNum) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, -1 * beforeNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    /**
     * 得到指定日期前beforeNum天的日期
     *
     * @param dateString
     *            以"yyyy-MM-dd"格式指定的日期
     * @param beforeNum
     *            提前量
     * @return String
     */
    public static String getDateByBefore(String dateString, int beforeNum) {
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
            calendar.setTime(date);
        } catch (Exception e) {
            return "";
        }
        calendar.add(6, -1 * beforeNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    /**
     * 得到指定日期后afterNum天的日期
     *
     * @param dateString
     *            以"yyyy-MM-dd"格式指定的日期
     * @param afterNum
     *            偏移量
     * @return String
     */
    public static String getDateByAfter(String dateString, int afterNum) {
        Calendar calendar = Calendar.getInstance();
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
            calendar.setTime(date);
        } catch (Exception e) {
            return "";
        }
        calendar.add(6, afterNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    public static String getWeekdayByBefore(int beforeNum) {
        Calendar now = Calendar.getInstance();
        now.add(6, -1 * beforeNum);
        return (new SimpleDateFormat("E")).format(now.getTime());
    }

    public static String getWeekdayByBefore(int year, int month, int day,
                                            int beforeNum) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, -1 * beforeNum);
        return (new SimpleDateFormat("E")).format(calendar.getTime());
    }

    public static String getDateByAfter(int afterNum) {
        Calendar now = Calendar.getInstance();
        now.add(6, afterNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
    }

    public static String getDateByAfter(int year, int month, int day,
                                        int afterNum) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, afterNum);
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    public static String getWeekdayByAfter(int afterNum) {
        Calendar now = Calendar.getInstance();
        now.add(6, afterNum);
        return (new SimpleDateFormat("E")).format(now.getTime());
    }

    public static String getWeekdayByAfter(int year, int month, int day,
                                           int afterNum) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, afterNum);
        return (new SimpleDateFormat("E")).format(calendar.getTime());
    }

    public static String getDateOfWeekend() {
        Calendar now = Calendar.getInstance();
        now.add(6, 7 - now.get(7));
        return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
    }

    public static String getDateOfWeekend(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, 7 - calendar.get(7));
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    public static String getDateOfWeekstart() {
        Calendar now = Calendar.getInstance();
        now.add(6, 1 - now.get(7));
        return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
    }

    public static String getDateOfWeekstart(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        calendar.add(6, 1 - calendar.get(7));
        return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
    }

    public static int getDateOfMonthend() {
        Calendar now = Calendar.getInstance();
        return now.getActualMaximum(5);
    }

    public static int getDateOfMonthend(int year, int month, int day) {
        GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
        return calendar.getActualMaximum(5);
    }

    public static String getDateBefore(String timeString, int minute) {
        long min = 0L;
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse(timeString);
            min = date.getTime();
        } catch (Exception exception) {
        }
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(
                min - (minute * 60 * 1000)));
    }

    public static boolean IsOverTime(String timeString, int rating) {
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse(timeString);
            Date now = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                            .format(new Date()));
            long min = date.getTime();
            long nowmin = now.getTime();
            return nowmin - min > (rating * 60 * 1000);
        } catch (Exception e) {
            return false;
        }
    }

    public static String switchDateFormat(String dateStr) {
        Date date = null;
        try {
            date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateStr);
        } catch (Exception exception) {
        }
        return (new SimpleDateFormat("yyyy\u5E74M\u6708d\u65E5")).format(date);
    }

    public static String switchDateFormat(String firstFormat,
                                          String secondFormat, String dateStr) {
        Date date = null;
        try {
            date = (new SimpleDateFormat(firstFormat)).parse(dateStr);
        } catch (Exception exception) {
        }
        return (new SimpleDateFormat(secondFormat)).format(date);
    }

    public static String getWeekdayByDateString(String dateString) {
        String weekday = "";
        try {
            Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
            weekday = (new SimpleDateFormat("E")).format(date);
        } catch (Exception exception) {
        }
        return weekday;
    }

    public static String getWeekdayByDate(Date date) {
        return (new SimpleDateFormat("E")).format(date);
    }

    public static String getDateTimeZone() {
        return (new SimpleDateFormat("yyyyMMddHHmmssS")).format(new Date());
    }

    public static String getYear() {
        return (new SimpleDateFormat("yyyy")).format(new Date());
    }

    /**
     * 根据传入yyyy-MM-dd HH:mm:ss格式的日期,获得季度<br>
     * 返回String数组 分别存放当前季度的开始时间,结束时间,当前季度,当前季度的中文形式<br>
     * 例如:
     *
     * <pre>
     *  2008-10-01
     *  2008-12-31
     *  4
     *  2008年第4季度
     * </pre>
     *
     * @param date
     *            yyyy-MM-dd HH:mm:ss格式的日期
     * @return
     */
    public static String[] getQuarter(Date date) {
        String record[] = new String[4];
        String startDateStr = "";
        String endDateStr = "";
        String quarter = null;
        String year = (new SimpleDateFormat("yyyy")).format(date);
        String month = (new SimpleDateFormat("MM")).format(date);
        int monthInteger = Integer.parseInt(month);
        if (monthInteger >= 1 && monthInteger <= 3) {
            startDateStr = "-01-01";
            endDateStr = "-03-31";
            quarter = "1";
        }
        if (monthInteger >= 4 && monthInteger <= 6) {
            startDateStr = "-04-01";
            endDateStr = "-06-30";
            quarter = "2";
        }
        if (monthInteger >= 7 && monthInteger <= 9) {
            startDateStr = "-07-01";
            endDateStr = "-09-30";
            quarter = "3";
        }
        if (monthInteger >= 9 && monthInteger <= 12) {
            startDateStr = "-10-01";
            endDateStr = "-12-31";
            quarter = "4";
        }
        record[0] = year + startDateStr;
        record[1] = year + endDateStr;
        record[2] = quarter;
        record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
        return record;
    }

    /**
     * 获得当前季度的相关信息<br>
     * 返回String数组 分别存放当前季度的开始时间,结束时间,当前季度,当前季度的中文形式<br>
     * 例如:
     *
     * <pre>
     *  2008-10-01
     *  2008-12-31
     *  4
     *  2008年第4季度
     * </pre>
     *
     * @return
     */
    public static String[] getNowQuarter() {
        String record[] = new String[4];
        String startDateStr = "";
        String endDateStr = "";
        String quarter = "";
        Date date = new Date();
        String year = (new SimpleDateFormat("yyyy")).format(date);
        String month = (new SimpleDateFormat("MM")).format(date);
        int monthInteger = Integer.parseInt(month);
        if (monthInteger >= 1 && monthInteger <= 3) {
            startDateStr = "-01-01";
            endDateStr = "-03-31";
            quarter = "1";
        }
        if (monthInteger >= 4 && monthInteger <= 6) {
            startDateStr = "-04-01";
            endDateStr = "-06-30";
            quarter = "2";
        }
        if (monthInteger >= 7 && monthInteger <= 9) {
            startDateStr = "-07-01";
            endDateStr = "-09-30";
            quarter = "3";
        }
        if (monthInteger >= 9 && monthInteger <= 12) {
            startDateStr = "-10-01";
            endDateStr = "-12-31";
            quarter = "4";
        }
        record[0] = year + startDateStr;
        record[1] = year + endDateStr;
        record[2] = quarter;
        record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
        return record;
    }

    public static String[] getDownQuarter(String date, String quarter)
            throws Exception {
        String record[] = new String[4];
        String startDateStr = "";
        String endDateStr = "";
        int monthInteger = Integer.parseInt(quarter);
        String year = (new SimpleDateFormat("yyyy"))
                .format((new SimpleDateFormat("yyyy-MM-dd")).parse(date));
        int yearInteger = Integer.parseInt(year);
        if (monthInteger == 1) {
            startDateStr = year + "-04-01";
            endDateStr = year + "-06-30";
            quarter = "2";
        }
        if (monthInteger == 2) {
            startDateStr = year + "-07-01";
            endDateStr = year + "-09-30";
            quarter = "3";
        }
        if (monthInteger == 3) {
            startDateStr = year + "-10-01";
            endDateStr = year + "-12-31";
            quarter = "4";
        }
        if (monthInteger == 4) {
            startDateStr = (yearInteger + 1) + "-01-01";
            endDateStr = (yearInteger + 1) + "-03-31";
            quarter = "1";
            year = Integer.toString(yearInteger + 1);
        }
        record[0] = startDateStr;
        record[1] = endDateStr;
        record[2] = quarter;
        record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
        return record;
    }

    public static String[] getUpQuarter(String date, String quarter)
            throws Exception {
        String record[] = new String[4];
        String startDateStr = "";
        String endDateStr = "";
        int monthInteger = Integer.parseInt(quarter);
        String year = (new SimpleDateFormat("yyyy"))
                .format((new SimpleDateFormat("yyyy-MM-dd")).parse(date));
        int yearInteger = Integer.parseInt(year);
        if (monthInteger == 1) {
            startDateStr = (yearInteger - 1) + "-10-01";
            endDateStr = (yearInteger - 1) + "-12-31";
            quarter = "4";
            year = Integer.toString(yearInteger - 1);
        }
        if (monthInteger == 2) {
            startDateStr = year + "-01-01";
            endDateStr = year + "-03-31";
            quarter = "1";
        }
        if (monthInteger == 3) {
            startDateStr = year + "-04-01";
            endDateStr = year + "-06-30";
            quarter = "2";
        }
        if (monthInteger == 4) {
            startDateStr = year + "-07-01";
            endDateStr = year + "-09-30";
            quarter = "3";
        }
        record[0] = startDateStr;
        record[1] = endDateStr;
        record[2] = quarter;
        record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
        return record;
    }

    public static int getExamTime(String firsttime, String secondtime, int m) {
        long record = 0L;
        try {
            Date first = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse(firsttime);
            Date second = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse(secondtime);
            Date now = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .parse((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                            .format(new Date()));
            long m_first = first.getTime();
            long m_second = second.getTime();
            if (m_first == m_second) {
                record = m * 60 * 1000;
            } else {
                record = (m * 60 * 1000) - (now.getTime() - first.getTime());
            }
        } catch (Exception exception) {
        }
        return (int) record / 60000;
    }

    public static String switchDateStr(String date) {
        String record = "";
        try {
            Date first = (new SimpleDateFormat("yyyy-MM-dd HH")).parse(date);
            record = (new SimpleDateFormat("yyyyMMddHH")).format(first);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return record;
    }

    public static String switchDateStrForWorkList(String date) {
        String record = "";
        try {
            Date first = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"))
                    .parse(date);
            record = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .format(first);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return record;
    }

    public static String uniteDateTime(String date, String time) {
        return date + " " + time;
    }

    public static String getOffsetTime(String format, int field, int offset) {
        Calendar calendar = Calendar.getInstance();

        calendar.add(field, offset);
        return new SimpleDateFormat(format).format(calendar.getTime());
    }

    public static String getOffsetTime(String timeString, String format,
                                       int field, int offset) {
        Calendar calendar = Calendar.getInstance();

        calendar.add(field, offset);
        return new SimpleDateFormat(format).format(calendar.getTime());
    }

    public static String getDatePattern() {
        return DATEPATTERN;
    }

    public static final String getDate(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(DATEPATTERN);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static final Date convertStringToDate(String aMask, String strDate)
            throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        if (log.isDebugEnabled()) {
            log.debug("converting '" + strDate + "' to date with mask '"
                    + aMask + "'");
        }

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            // log.error("ParseException: " + pe);
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }

        return (date);
    }

    /**
     * 把传入的字符串转换为Date
     *
     * @param aMask
     *            传入的字符串的格式
     * @param strDate
     *            时间日期字符串
     * @return
     * @throws java.text.ParseException
     */
    public static final Date convertStringToDateTime(String aMask,
                                                     String strDate) throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        if (log.isDebugEnabled()) {
            log.debug("converting '" + strDate + "' to date time with mask '"
                    + aMask + "'");
        }

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            // log.error("ParseException: " + pe);
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }

        return (date);
    }

    /**
     * This method returns the current date time in the format: yyyy/MM/dd HH:MM
     * a
     *
     * @param theTime
     *            the current time
     * @return the current date/time
     */
    public static String getTimeNow(Date theTime) {
        return getDateTime(TIMEPATTERN, theTime);
    }

    /**
     * This method returns the current date in the format: yyyy-MM-dd
     *
     * @return the current date
     * @throws java.text.ParseException
     */
    public static Calendar getToday() throws ParseException {
        Date today = new Date();
        SimpleDateFormat df = new SimpleDateFormat(DATEPATTERN);

        // This seems like quite a hack (date -> string -> date),
        // but it works ;-)
        String todayAsString = df.format(today);
        Calendar cal = new GregorianCalendar();
        cal.setTime(convertStringToDate(todayAsString));

        return cal;
    }

    /**
     * This method generates a string representation of a date's date/time in
     * the format you specify on input
     *
     * @param aMask
     *            the date pattern the string is in
     * @param aDate
     *            a date object
     * @return a formatted string representation of the date
     * @see java.text.SimpleDateFormat
     */
    public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            log.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    /**
     *
     * @param aDate
     * @return
     */
    public static final String getDateTime(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (aDate == null) {
            log.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(DATETIMEPATTERN);
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    public static final String convertDateToString(Date aDate) {
        return getDateTime(DATEPATTERN, aDate);
    }

    public static Date convertStringToDate(String strDate)
            throws ParseException {
        Date aDate = null;

        try {
            if (log.isDebugEnabled()) {
                log.debug("converting date with pattern: " + DATEPATTERN);
            }

            aDate = convertStringToDate(DATEPATTERN, strDate);
        } catch (ParseException pe) {
            log.error("Could not convert '" + strDate
                    + "' to a date, throwing exception");
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());

        }

        return aDate;
    }

    /**
     * 将传入的字符串转换为Date类型
     *
     * @param strDate
     *            需要转换的字符串,格式为'yyyy-MM-dd HH:mm:ss'
     * @return
     * @throws java.text.ParseException
     */
    public static Date convertStringToDateTime(String strDate)
            throws ParseException {
        Date aDate = null;

        try {
            if (log.isDebugEnabled()) {
                log.debug("converting date with pattern: " + DATETIMEPATTERN);
            }

            aDate = convertStringToDate(DATETIMEPATTERN, strDate);
        } catch (ParseException pe) {
            log.error("Could not convert '" + strDate
                    + "' to a date, throwing exception");
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());

        }

        return aDate;
    }

    /**
     * 将指定的日期减少指定的天数
     *
     * @param dateTime
     * @return
     * @throws java.text.ParseException
     */
    public static String getSubtractTime(String dateTime, int day) {
        String nowTime = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date validToDate = sdf.parse(dateTime);
            Calendar calendar1 = Calendar.getInstance();// 日历对象
            calendar1.setTime(validToDate);// 设置该公司服务到期的日期
            calendar1.add(Calendar.DAY_OF_MONTH, day);
            nowTime = sdf.format(calendar1.getTime());
        } catch (Exception e) {
            log.error("将指定的日期减少指定的天数失败", e);
        }
        return nowTime;
    }

    /**
     * 获取2个日期相差天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getOffset(String date1, String date2) {
        long diff = 0;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date d1 = sdf.parse(date1);
            Date d2 = sdf.parse(date2);

            long c = d2.getTime() - d1.getTime();
            diff = c / 1000 / 3600 / 24;
        } catch (Exception e) {
            log.error("获取偏移量失败", e);
        }

        return diff;
    }

    public static void main(String[] args) {
        String d = "2011-11-03";
        String d2 = "2011-11-01";
        long date = DateTimeUtils.getOffset(d, d2);

        System.out.println(date);

    }

    public static String getTimeZone(){
        SimpleDateFormat sdf = new SimpleDateFormat(TIMEZONEPATTERN);
        return sdf.format(new Date());
    }

}

MD5Util.java代码:

package com.base.util;

import java.security.MessageDigest;

public class MD5Util {
    public final static String encode(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.encode("123456"));
//        System.out.println(Integer.toHexString(655350));
    }
}

CookieUtils.java代码:

package com.base.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtils {
    public static void setCookie(HttpServletResponse response, String name, String value, String domain, int maxAge) {
        if (value == null) {
            value = "";
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        if (domain != null && !"".equals(domain)) {
            cookie.setDomain(domain);
        }
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        // Return null if there are no cookies or the name is invalid.
        if (cookies == null || name == null || name.length() == 0) {
            return null;
        }
        // Otherwise, we do a linear scan for the cookie.
        Cookie cookie = null;
        for (int i = 0; i < cookies.length; i++) {
            // If the current cookie name matches the one we're looking for,
            // we've
            // found a matching cookie.
            if (cookies[i].getName().equals(name)) {
                cookie = cookies[i];
                // The best matching cookie will be the one that has the correct
                // domain name. If we've found the cookie with the correct
                // domain name,
                // return it. Otherwise, we'll keep looking for a better match.
                break;
            }
        }
        return cookie;
    }
}

未完待续

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值