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

📄 format.java

📁 本程序是作者开发的一个宽带娱乐系统的一个模块.
💻 JAVA
字号:
package com.singnet.util;

/**
 * 一些非常有用的方法
 * @author openheart
 * @ver 1.0
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.org.apache.regexp.internal.RE;

public class Format {
    String re;
    String gb;
    String datestr;
    boolean ok;
    /**
     * 获取当前时间
     * @return String
     */
    public static String getDateTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date Now = new java.util.Date();
        String NDate = formatter.format(Now);
        return NDate;
    }

    /**
     * 只去年月日的日期转换
     * @param DateString String
     * @return String
     */
    public static String getStrDate(String DateString) {
        return DateString.substring(0, 10);
    }

    /**
     * 字符到日期的转换
     * @param dateStr String
     * @return Date
     */
    public java.sql.Date stringToDate(String dateStr) {
        if (dateStr == null || dateStr.length() < 1) {
            return java.sql.Date.valueOf("1999-9-9");
        }
        return java.sql.Date.valueOf(dateStr);
    }

    /**
     * 验证时间,格式2005-02-05
     * @param str String
     * @return boolean
     */
    public boolean isValidDate(String str) {
        try {
            RE date = new RE("^(\\d\\d\\d\\d)-(\\d{1,2})-(\\d{1,2})$");
            return (date.match(str));
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return false;
        }
    }

    /**
     * 验证电话号码
     * @param str String
     * @param type String
     * @return boolean
     */
    public boolean isValidPhone(String str, String type) {
        try {
            if (type.equals("mobile")) {
                RE phone = new RE("^[0-9]{11,11}$");
                return (phone.match(str));
            } else {
                if (type.equals("phone")) {
                    RE phone = new RE("^(\\d{3,4})-(\\d{6,8})$");
                    return (phone.match(str));
                } else {
                    return false;
                }
            }
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return false;
        }
    }

    /**
     * 比较时间先后
     * @param last String
     * @param now String
     * @return boolean
     */
    public static boolean compareTo(String last, String now) {
        try {
            DateFormat formatter = DateFormat.getDateInstance();
            Date temp1 = formatter.parse(last);
            Date temp2 = formatter.parse(now);
            if (temp1.after(temp2)) {
                return false;
            } else if (temp1.before(temp2)) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 字符分割函数
     * @param source String
     * @param div String
     * @return String[]
     */
    public static String[] split(String source, String div) {
        int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
        if (source.compareTo("") != 0) {
            if (source.indexOf(div) != -1) {
                intIdx = source.indexOf(div);
                for (int intCount = 1; ; intCount++) {
                    if (source.indexOf(div, intIdx + div_length) != -1) {
                        intIdx = source.indexOf(div, intIdx + div_length);
                        arynum = intCount;
                    } else {
                        arynum += 2;
                        break;
                    }
                }
            } else {
                arynum = 1;
            }
        } else {
            arynum = 0;

        }
        intIdx = 0;
        intIdex = 0;
        String[] returnStr = new String[arynum];

        if (source.compareTo("") != 0) {
            if (source.indexOf(div) != -1) {
                intIdx = (int) source.indexOf(div);
                returnStr[0] = (String) source.substring(0, intIdx);
                for (int intCount = 1; ; intCount++) {
                    if (source.indexOf(div, intIdx + div_length) != -1) {
                        intIdex = (int) source.indexOf(div, intIdx + div_length);
                        returnStr[intCount] = (String) source.substring(intIdx +
                                div_length,
                                intIdex);
                        intIdx = (int) source.indexOf(div, intIdx + div_length);
                    } else {
                        returnStr[intCount] = (String) source.substring(intIdx +
                                div_length,
                                source.length());
                        break;
                    }
                }
            } else {
                returnStr[0] = (String) source.substring(0, source.length());
                return returnStr;
            }
        } else {
            return returnStr;
        }
        return returnStr;
    }

    /**
     * 处理为空的情况 null->""
     * @param str String
     * @return String
     */
    public static String dealNull(String str) {
        String returnstr = null;
        if (str == null) {
            returnstr = "";
        } else {
            returnstr = str;
        }
        return returnstr;
    }

    /**
     *
     * 替换字符函数
     * @param str String
     * @param substr String
     * @param restr String
     * @return String
     */
    public static String replace(String str, String substr, String restr) {
        String[] tmp = split(str, substr);
        String returnstr = null;
        if (tmp.length != 0) {
            returnstr = tmp[0];
            for (int i = 0; i < tmp.length - 1; i++) {
                returnstr = dealNull(returnstr) + restr + tmp[i + 1];
            }
        }
        return dealNull(returnstr);
    }

    /**
     * 转换 sql
     * @param str String
     * @return String
     */
    public static String toSql(String str) {
        String sql = new String(str);
        return replace(sql, "'", "''");
    }

    public String toGb(String uniStr) {
        String gbStr = "";
        if (uniStr == null) {
            uniStr = "";
        }
        try {
            byte[] tempByte = uniStr.getBytes("ISO8859_1");
            gbStr = new String(tempByte, "GBK");
        } catch (Exception ex) {
        }
        return gbStr;
    }

    /**
     * 是否为字母
     * @param validString String
     * @return boolean
     */
    public static boolean isLetter(String validString) {
        byte[] tempbyte = validString.getBytes();
        for (int i = 0; i < validString.length(); i++) {
            //by=tempbyte[i];
            if ((tempbyte[i] < 65) || (tempbyte[i] > 122) ||
                ((tempbyte[i] > 90) & (tempbyte[i] < 97))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否为字符
     * @param validString String
     * @return boolean
     */
    public static boolean isChar(String validString) {
        byte[] tempbyte = validString.getBytes();
        for (int i = 0; i < validString.length(); i++) {
            //  by=tempbyte[i];
            if ((tempbyte[i] < 48) || ((tempbyte[i] > 57) & (tempbyte[i] < 65)) ||
                (tempbyte[i] > 122) || ((tempbyte[i] > 90) & (tempbyte[i] < 95)) ||
                ((tempbyte[i] > 95) & (tempbyte[i] < 97))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否为数字
     * @param validString String
     * @return boolean
     */
    public static boolean isNumber(String validString) {
        byte[] tempbyte = validString.getBytes();
        for (int i = 0; i < validString.length(); i++) {
            //by=tempbyte[i];
            if ((tempbyte[i] < 48) || (tempbyte[i] > 57)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEamil(String email) {
        if (email.indexOf("@") < 1 || email.indexOf(".") < 1) {
            return false;
        }
        return true;
    }

    /**
     * 是否为boolean
     * @param str String
     * @return boolean
     */
    public boolean isValidBoolean(String str) {
        try {
            if (str.equals("0") || str.equals("1")) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return false;
        }
    }

    /**
     * 是否为密码
     * @param str String
     * @return boolean
     */
    public static boolean isValidPassword(String str) {
        try {
            if (str.length() < 6) {
                return false;
            } else {
                return true;
            }
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
            return false;
        }
    }

    public static String getAuthCode() {
        String[] code = {
                        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a",
                        "b", "c", "d",
                        "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
                        "p", "q", "r",
                        "s", "t", "u", "v", "w", "x", "y", "z"};
        int len = code.length;
        int n = 0;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4; i++) {
            n = (int) (Math.random() * len);
            sb.append(code[n]);
        }
        return sb.toString();
    }


    public static String fotmatDate3(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate4(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate5(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate6(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    public static String fotmatDate7(Date myDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String strDate = formatter.format(myDate);
        return strDate;
    }

    /**
     * 验证数字
     * @param str String
     * @return boolean
     */
    public boolean isValidNum(String str) {
        try {
            RE date = new RE("^[0-9]+$");
            return (date.match(str));
        } catch (Exception ex) {
            System.out.print("Baseform Error" + ex);
            return false;
        }
    }

}

⌨️ 快捷键说明

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