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

📄 stringutil.java

📁 一个java的成熟的论坛模式
💻 JAVA
字号:
package org.lxd.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class StringUtil {
	public static void main(String[] args) {

		System.out.println(getTimeMillis());

	}

	public static String getTimeMillis(){
		return System.currentTimeMillis()+"";
	}
	//  将字符串转换成GBK编码
	public static String toGBK(String value) {
		try {
			if (null != value)
				value = new String(value.getBytes("GBK"), "ISO8859_1");
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return value;
	}
	
//  将字符串转换成UTF-8编码
	public static String toUtf8(String value) {
		try {
			if (null != value)
				value = new String(value.getBytes("ISO8859_1"), "utf-8");
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return value;
	}
	
	public static String toGb2312(String str){
		String temp_str ="";
		try {
			temp_str = new String(str.getBytes("iso_8859_1"),"gb2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp_str;
	}
	// 转换内码到ISO
	public static String toISO(String input) {
		try {
			if (null != input && input.indexOf("null")<0) {
				byte[] bytes = input.getBytes("ISO8859_1");
				input = new String(bytes);
			}
			else{
				input = "";
			}

		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return input;
	}
	

	
//	加密算法
    private static MessageDigest digest = null;
    public synchronized static final String hash(String data) {
        if (digest == null) {
            try {
                digest = MessageDigest.getInstance("MD5");
            }
            catch (NoSuchAlgorithmException nsae) {
                System.err.println("Failed to load the MD5 MessageDigest." );
                nsae.printStackTrace();
            }
        }

        digest.update(data.getBytes());
        return encodeHex(digest.digest());
    }
    public static final String encodeHex(byte[] bytes) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        int i;

        for (i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) bytes[i] & 0xff, 16));
        }
        return buf.toString();
    }
    
    
	public static String isEmpty(String input) {
		if (null == input || input.indexOf("null")>=0) return "";
		return input;
	}	
	
    public static String htmlEncoder(String src) throws Exception
    {
		String sTemp;
		sTemp = src;
		if(sTemp == null || sTemp.equals(""))
		{
			return "";
		}
		sTemp = sTemp.replaceAll("&", "&amp;");
		sTemp = sTemp.replaceAll("<", "&lt;");
		sTemp = sTemp.replaceAll(">", "&gt;");
		sTemp = sTemp.replaceAll("\"", "&quot;");
		return sTemp;
    }
    
    public static String getRandomString(){
    	int r = (int)(Math.random()*1000);
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyMdhms");
		String str = sf.format(date);
		return str+r;
    }
    
    public static String cutStrToDateStr(String str){
    	if(str==null ||str.equals("")){
    		str = "-";
    	}else{
    		if(str.length()>10){
    			str = str.substring(0,10);
    		}
    	}
    	return str;
    }
    
    
//	输入的字符串必须要是浮点类型的
	public static String changeToBig(double value){
	    char[] hunit={'拾','佰','仟'};                                               //段内位置表示
	    char[] vunit={'万','亿'};                                                     //段名表示
	    char[] digit={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};  //数字表示
	    long midVal = (long)(value*100);                                      //转化成整形
	    String valStr=String.valueOf(midVal);                                //转化成字符串
	    String head=valStr.substring(0,valStr.length()-2);               //取整数部分
	    String rail=valStr.substring(valStr.length()-2);                     //取小数部分

	    String prefix="";                                                                 //整数部分转化的结果
	    String suffix="";                                                                  //小数部分转化的结果
	    //处理小数点后面的数
	    if(rail.equals("00")){                                                            //如果小数部分为0
	      suffix="整";
	    } else{
	      suffix=digit[rail.charAt(0)-'0']+"角"+digit[rail.charAt(1)-'0']+"分";        //否则把角分转化出来
	    }
	    //处理小数点前面的数
	    char[] chDig=head.toCharArray();                                                         //把整数部分转化成字符数组
	    char zero='0';                                                                                          //标志'0'表示出现过0
	    byte zeroSerNum = 0;                                                                            //连续出现0的次数
	    for(int i=0;i<chDig.length;i++){                                                               //循环处理每个数字
	      int idx=(chDig.length-i-1)%4;                                                                //取段内位置
	      int vidx=(chDig.length-i-1)/4;                                                                //取段位置
	      if(chDig[i]=='0'){                                                                                  //如果当前字符是0
	        zeroSerNum++;                                                                                 //连续0次数递增
	        if(zero == '0'){                                                                                    //标志
	          zero=digit[0];
	        } else if(idx==0 && vidx >0 &&zeroSerNum < 4){
	          prefix += vunit[vidx-1];
	          zero='0';
	        }
	        continue;
	      }
	      zeroSerNum = 0;                                                                                    //连续0次数清零
	      if(zero != '0') {                                                                                        //如果标志不为0,则加上,例如万,亿什么的
	        prefix+=zero;
	        zero='0';
	      }
	      prefix+=digit[chDig[i]-'0'];                                                                        //转化该数字表示
	      if(idx > 0) prefix += hunit[idx-1];                  
	      if(idx==0 && vidx>0){
	        prefix+=vunit[vidx-1];                                                                             //段结束位置应该加上段名如万,亿
	      }
	    }

	    if(prefix.length() > 0) prefix += '圆';                                                             //如果整数部分存在,则有圆的字样
	    return prefix+suffix;                                                                                     //返回正确表示
	  }
	
	public static String convertStrToGBK(String s) {
	    try {
		    return new String(s.getBytes("iso-8859-1"),"GBK");
		} catch (Exception e) {
            return null;
        }
	}
	
	//获得模糊查询的字符串
	public static String getLikeString(String s) {
   	    return "%" + (s == null ? removeNull(s).trim() : s.trim()) + "%";
	}

	/**
	 * 判断对象是否为空,如果为空,返回“”; 如果不为空,返回对象的toString()
	 * @param o    被判断的对象
	 * @return	
	 */
	public static String removeNull(Object o) {	
		return removeNull(o, "");
	}

	public static String removeNullInHtml(Object o) {
		if (o == null || o.toString().trim().equals("")) {
			return "&nbsp;";
		}
		
		return o.toString().trim();
	}
		
	/**
	 * 判断对象是否为空,如果为空,返回“”; 如果不为空,返回对象的toString()
	 * @param o 	被判断的对象
	 * @param s	默认值	
	 * @return	
	 */
	public static String removeNull(Object o, String s) {
		if (o == null) {
			return s;
		}
		
		return o.toString().trim();
	}
	
	
	public static int getYear() {
		return getYear(new Date());
	}
	
	public static int getYear(Date d) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.get(Calendar.YEAR);
	}	
	
	public static int getMonth() {
		return getMonth(new Date());
	}
	
	public static int getMonth(Date d) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		return c.get(Calendar.MONTH) + 1;
	}
	
	public static boolean isStringInArray(String[] array, String s) {
		for (int i = 0; i < array.length; i ++) {
			if (array[i].equals(s)) 
				return true;	
		}
		
		return false;
	}
	
	/**
	 * 取得格式为"yyyy-MM-dd"的日期字符串
	 * @param d 要格式化的日期
	 * @return
	 */	
	public static String getDateString(Date d) {
    	return getDateString(d, "yyyy-MM-dd");
	}
	
	/**
	 * 取得格式为"yyyy-MM-dd HH:mm:ss"的日期字符串
	 * @param d 要格式化的日期
	 * @return
	 */	
	public static String getGeneralDateString(Date d) {
    	return getDateString(d, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 将日期转换成制定格式的字符串
	 * @param d	   要格式的日期	
	 * @param pattern 日期的格式
	 * @return
	 */
	public static String getDateString(Date d, String pattern) {
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    	return sdf.format(d);
	}	

	
	/** 如果字符串的位数为1,在字符串的左边添加0
	 * @param s	字符串
	 * @return		处理后的字符串		
	 */
	public static String addZero(String s) {
		if (s.length() == 1) {
			s = "0" + s;
		}		
		return s;			
	}

	/**
	 * 如果整数的位数为1,在整数的左边添加0
	 * @param i	整数
	 * @return		处理后的字符串
	 */
	public static String addZero(int i) {
		return addZero(String.valueOf(i));
	}
	
	
}

⌨️ 快捷键说明

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