⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolutil.java

📁 高效海量访问系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * <p>Title: 通用函数CB为 CommonBase的缩写</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: sohu</p>
 *
 * @author wangjm
 * @version 1.0
 * @since 2003/04/09
 */

public class ToolUtil {
    
    static Logger log = Logger.getLogger(ToolUtil.class);

    public ToolUtil() {
    }

    /**
     * 计算当前距离计费周期截至日相差天数
     *
     * @param periodendday int
     * @return int
     * @author zhangyx
     * @since 2004/06/03
     */
    public static int daysToPeriodEnd(int periodendday) {
        int daycount = 0;
        String sdate = null;
        try {
            sdate = ToolUtil.dateToStr(ToolUtil.getDate());
        } catch (Exception ex) {
        }
        sdate = sdate.substring(sdate.length() - 2, sdate.length());
        int day = Integer.parseInt(sdate);
        if (day < periodendday) {
            daycount = periodendday - day;
        } else {
            daycount = 30 - (day - periodendday);
        }
        return daycount;
    }

    /**
     * 判断当前日期和指定日期是否在同一个计费周期内(20-21)
     *
     * @param enddate java.util.Date
     * @return boolean  (true:表示在一个周期内false:表示不在一个周期内)
     * @author wangjm
     * @since 2003/04/09
     */
    public static boolean IsSamePeriod(java.util.Date enddate) {
        boolean is = false;
        int sday = 0;
        long StartD = 0;
        long EndD = 0;
        long CurrD = 0;
        Calendar MyDate = Calendar.getInstance();
        MyDate.setTime(enddate);
        sday = MyDate.get(Calendar.DAY_OF_MONTH);
        MyDate.set(Calendar.HOUR_OF_DAY, 0);
        MyDate.set(Calendar.MINUTE, 0);
        MyDate.set(Calendar.SECOND, 0);
        if (sday <= 20) {
            MyDate.set(Calendar.DAY_OF_MONTH, 21);
            EndD = MyDate.getTimeInMillis();
            MyDate.set(Calendar.MONTH, MyDate.get(Calendar.MONTH) - 1);
            MyDate.set(Calendar.DAY_OF_MONTH, 20);
            StartD = MyDate.getTimeInMillis();
        } else if (sday > 20) {
            MyDate.set(Calendar.DAY_OF_MONTH, 20);
            StartD = MyDate.getTimeInMillis();
            MyDate.set(Calendar.MONTH, MyDate.get(Calendar.MONTH) + 1);
            MyDate.set(Calendar.DAY_OF_MONTH, 21);
            EndD = MyDate.getTimeInMillis();
        }
        MyDate.setTime(new java.util.Date());
        log.debug("计费周期开始日期:" + new java.util.Date(StartD));
        log.debug("计费周期结束日期:" + new java.util.Date(EndD));
        CurrD = MyDate.getTimeInMillis();

        if (CurrD >= StartD && CurrD <= EndD) {
            is = true;
        }
        return is;
    }

    /**
     * 获得免费使用的截至日期(取所在的计费周期的截至日期-21号)
     *
     * @param enddate java.util.Date
     * @return java.util.Date
     * @author wangxi
     * @since 2003/09/14
     */
    public static java.util.Date getPeriodDate(java.util.Date thisdate) {
        int sday = 0;
        long StartD = 0;
        long EndD = 0;
        long CurrD = 0;
        Calendar MyDate = Calendar.getInstance();
        MyDate.setTime(thisdate);
        sday = MyDate.get(Calendar.DAY_OF_MONTH);
        MyDate.set(Calendar.HOUR_OF_DAY, 0);
        MyDate.set(Calendar.MINUTE, 0);
        MyDate.set(Calendar.SECOND, 0);
        if (sday <= 20) {
            MyDate.set(Calendar.DAY_OF_MONTH, 21);
            EndD = MyDate.getTimeInMillis();
            MyDate.set(Calendar.MONTH, MyDate.get(Calendar.MONTH) - 1);
            MyDate.set(Calendar.DAY_OF_MONTH, 20);
            StartD = MyDate.getTimeInMillis();
        } else if (sday > 20) {
            MyDate.set(Calendar.DAY_OF_MONTH, 20);
            StartD = MyDate.getTimeInMillis();
            MyDate.set(Calendar.MONTH, MyDate.get(Calendar.MONTH) + 1);
            MyDate.set(Calendar.DAY_OF_MONTH, 21);
            EndD = MyDate.getTimeInMillis();
        }
        MyDate.setTime(new java.util.Date());
        log.debug("计费周期开始日期:" + new java.util.Date(StartD));
        log.debug("计费周期结束日期:" + new java.util.Date(EndD));
        // CurrD = MyDate.getTimeInMillis();

        return new java.util.Date(EndD);
    }

    /**
     * 得到一个N位的随机数
     *
     * @param Max int
     * @param len int
     * @return	处理后的串 String
     * @author wangjm
     * @since 2003/04/09
     */
    public static String getRandomString(int Max, int len) {
        StringBuffer _str = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            int ind = (int) (Math.random() * Max);
            _str.append(ind);
        }
        return _str.toString().length() > len ? _str.toString().substring(0, len) :
                _str.toString();
    }

    /**
     * 处理null转换成空串
     *
     * @param Col String
     * @return	处理后的串 String
     * @author wangjm
     * @since 2003/04/09
     */
    public static String NullToSp(String Col) {
        if (Col == null) {
            return "";
        } else {
            return Col;
        }
    }

    /**
     * 处理null转换成"0"
     *
     * @param Col String
     * @return	处理后的串 String
     * @author wangjm
     * @since 2003/04/09
     */
    public static String NullToZero(String Col) {
        if (Col == null || Col.equals("")) {
            return "0";
        } else {
            return Col.trim();
        }
    }

    /**
     * 得到系统时间
     *
     * @throws Exception 抛出系统异常
     * @return	返回值java.util.Date型系统时间
     * @author wangjm
     * @since 2003/04/09
     */
    public static java.util.Date getDate() throws Exception {
        java.util.Date date = null;
        Calendar MyDate = Calendar.getInstance();
        MyDate.setTime(new java.util.Date());
        date = MyDate.getTime();
        return date;
    }

    /**
     * 通过指定uitl 日期 得到sql 日期
     *
     * @param udate java.util.Date
     * @throws Exception 抛出系统异常
     * @return	返回值java.sql.Date型系统时间
     * @author wangjm
     * @since 2003/05/10
     */
    public static java.sql.Date getSqlDate(java.util.Date udate) {
        long idate = udate.getTime();
        java.sql.Date sqldate = new java.sql.Date(idate);
        return sqldate;
    }

    /**
     * 通过指定的字符串得到相应的sql日期
     *
     * @param sdate java.util.Date
     * @throws Exception 抛出系统异常
     * @return	返回值java.sql.Date型系统时间
     * @author wangjm
     * @since 2003/05/10
     */
    public static java.sql.Date getSqlDate(String sdate) {
        java.sql.Date sqldate = null;
        try {
            java.util.Date udate = strToDate(sdate);
            long idate = udate.getTime();
            sqldate = new java.sql.Date(idate);
        } catch (Exception e) {
            log.error("调用getSqlDate(String sdate)方法时异常" + e.toString());
        }
        return sqldate;
    }

    /**
     * 通过指定的字符串得到相应的日期
     *
     * @param nian String
     * @param yue  String
     * @param ri   String
     * @throws Exception 抛出系统异常
     * @return	返回值java.util.Date型系统时间
     * @author wangjm
     * @since 2003/05/10
     */
    public static java.util.Date getdate(String nian, String yue, String ri) throws
            Exception {
        Calendar MyDate = Calendar.getInstance();
        try {
            int year = Integer.parseInt(nian);
            int month = Integer.parseInt(yue) - 1;
            int date = Integer.parseInt(ri);
            MyDate.set(year, month, date);
        } catch (Exception e) {

            log.error("调用getdate(string,string,string)方法时异常:" + e.toString());
        }
        return MyDate.getTime();
    }

    /**
     * 通过指定的字符串得到相应的日期
     *
     * @param strDate String 字符串格式须为yyyy-MM-dd HH:mm:ss
     * @throws Exception 抛出系统异常
     * @return	返回值java.util.Date型时间
     */
    public static java.util.Date strToDate(String strDate) throws Exception {
        java.util.Date myDate = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (strDate != null && !strDate.equals(""))
                myDate = df.parse(strDate);
        } catch (Exception e) {
            log.error("调用strToDate(strDate)方法时异常:" + e.toString());
        }
        return myDate;
    }

    /**
     * 通过指定的字符串得到相应的日期
     *
     * @param strDate String 字符串格式须为合法类型
     * @throws Exception 抛出系统异常
     * @author wangxi
     * @Data 2004-06-02
     * @return	返回值java.util.Date型时间
     */
    public static java.util.Date speStrToDate(String strDate, String dateType) throws Exception {
        java.util.Date myDate = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(dateType);
            if (strDate != null && !strDate.equals(""))
                myDate = df.parse(strDate);
        } catch (Exception e) {
            log.error("调用strToDate(strDate)方法时异常:" + e.toString());
        }
        return myDate;
    }

    /**
     * 通过指定的日期得到相应的日期字符串
     *
     * @param date java.util.Date
     * @throws Exception 抛出系统异常
     * @return	返回值String型 yyyy-MM-dd
     */
    public static String dateToStr(java.util.Date date) {
        String strDate = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            strDate = df.format(date);
        }
        return strDate;
    }

    /**
     * 通过指定的日期得到相应的日期字符串
     *
     * @param date java.util.Date
     * @throws Exception 抛出系统异常
     * @return	返回值String型 yyyy-MM-dd HH:mm:ss
     */
    public static String dateToLongStr(java.util.Date date) {
        String strDate = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            strDate = df.format(date);
        }
        return strDate;
    }

    /**
     * 按指定日期格式得到系统时间
     *
     * @param format_date String
     * @throws Exception 抛出系统异常
     * @return	返回值String型系统时间如:yyyy-MM-dd HH:mm:ss;yyyyMMddHHmmss;yyyyMMdd
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static String getDate_A(String format_date) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat(format_date);
        Calendar MyDate = Calendar.getInstance();
        MyDate.setTime(new java.util.Date());
        String adddate = df.format(MyDate.getTime());
        return adddate;
    }

    /**
     * 按指定形式输出字符串
     *
     * @param	d 被处理的数据(double型参数)
     * @param	digit 保留小数点后的位数(int型参数)
     * @param	is boolean型参数(is:true 表示始终保留小数点后一位小数,is:false表示为整数时不保留小数点
     * @return	String 返回值为保留digit位小数的字符串的字符串
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static String Format_d(double d, int digit, boolean is) {
        String str = "";
        String sformat = "";
        try {
            for (int i = 0; i < digit; i++) {
                if (i == 0) {
                    if (is) {
                        sformat = ".0";
                    } else {
                        sformat = ".#";
                    }
                } else {
                    sformat = sformat + "#";
                }
            }
            DecimalFormat df = new DecimalFormat("#,##0" + sformat); //设置输出数值的格式为XX.XX
            str = df.format(d);
        } catch (Exception e) {
            log.error("调用Format_d方法时格式化数据时异常:" + e.toString());
            return d + "";
        }
        return str;
    }

    /**
     * 查找指定字符串在源字符串中出现的次数
     *
     * @param source String
     * @param sign   String
     * @return	返回值Int型 为含指定字符串次数
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static int getCount(String source, String sign) {
        //查找某一字符串中str,特定子串s的出现次数
        if (source == null) return 0;
        StringTokenizer s = new StringTokenizer(source, sign);
        return s.countTokens();
    }

    /**
     * 按指定字符串来分割源字符串返回数组
     *
     * @param source String
     * @param sign   String
     * @return	返回值数组型
     * @Exception
     * @author wangjm
     * @since 2003/04/09
     */
    public static String[] getArray(String source, String sign) {
        //按特定子串s为标记,将子串截成数组。
        int count = getCount(source, sign);
        int j = 0;
        String[] arr = new String[count];
        for (int i = 0; i < count; i++) {
            if (source.indexOf(sign) != -1) {
                j = source.indexOf(sign);
                arr[i] = source.substring(0, j);
                source = source.substring(j + 1);
            } else {
                arr[i] = source;
            }
        }
        return arr;

    }

    /**
     * 截取指定长度的一个字符串
     *
     * @param is (boolean true: 假如大与指定长度,一省略号提示, false:无论怎样都不以省略号形式显示)
     * @param	TxtData 类型(String)
     * @param	i 类型(int)
     * @return	 返回值(String)
     * @Exception
     * @author wangjm

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -