bean-woo 发表于 2013-2-3 10:39:29

常用类型转换

package com.core.util;
 
 
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.ParseException;
 
import org.apache.commons.lang.StringEscapeUtils;
 
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 
public class Utility {
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat sdfshort = new SimpleDateFormat(
"yyyy-MM-dd");
private static final Random randoms = new Random();
/**
* yyyy-MM-dd HH:mm:ss

* @param date
* @return
*/
public static String formatDate(Date date) {
if (date == null)
return "0000-00-00 00:00:00";
 
return sdf.format(date);
}
 
/**
* yyyy-MM-dd

* @param date
* @return
*/
public static String shortFormatDate(Date date) {
if (date == null)
return "0000-00-00";
 
return sdfshort.format(date);
}
 
/**
* yyyy-MM-dd HH:mm:ss

* @param str
* @return
* @throws ParseException
*/
public static Date parseDate(String str) {
if (str == null)
return null;
 
Date date = null;
 
try {
date = sdf.parse(str);
} catch (ParseException pe) {
}
 
return date;
}
 
/**
* 判断字符是否是数字

* @param str
* @return
*/
public static boolean isNumberString(String str) {
if (str == null || str.length() == 0)
return false;
 
int i = 0;
 
while (true) {
if (i >= str.length())
break;
 
char ch = str.charAt(i);
 
if (ch > '9' || ch < '0')
break;
 
i++;
}
 
return i == str.length();
}
 
/**
* 用户名为字母、数字或下划线、并不能以数字打头和纯数字

* @param userName
* @return
*/
public static boolean isUserName(String userName) {
boolean is = true;
String regEx = "^{1}(|){3,15}$";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(userName);
is = m.find();
return is;
}

/**
* 判断是否是邮箱地址

* @param eMail
* @return
*/
public static boolean isEmail(String eMail) {
boolean is = true;
String regEx = "^\\w+((-\\w+)|(\\.\\w+))*\\@+((\\.|-)+)*\\.+$";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(eMail);
is = m.find();
return is;
}
/**
* 防SQL注入

* @param sqlStr
* @return
*/
public static String escapeSql(String sqlStr) {
sqlStr = StringEscapeUtils.escapeSql(sqlStr);
return sqlStr;
}

/**
* 过滤request

* @param sqlStr
* @return
*/
public static String requestEscape(String str) {
if (str == null) {
return null;
}
str = Utility.escapeSql(str);
return str;
}

public static String wipescript(String html) { 
Pattern pattern1 = Pattern.compile("<script[^>]+>.*<\\/script>");
Pattern pattern2 = Pattern.compile(" href *= *[\\s\\s]*script *:");
Pattern pattern3 = Pattern.compile(" on[\\s\\s]*=");
Pattern pattern4 = Pattern.compile("<iframe[\\s\\s]+</iframe *>");
Pattern pattern5 = Pattern.compile("<frameset[\\s\\s]+</frameset *>");
Matcher matcher1 = pattern1.matcher(html);
html = matcher1.replaceAll(" ");
Matcher matcher2 = pattern2.matcher(html);
html = matcher2.replaceAll(" ");
Matcher matcher3 = pattern3.matcher(html);
html = matcher3.replaceAll(" ");
Matcher matcher4 = pattern4.matcher(html);
html = matcher4.replaceAll(" ");
Matcher matcher5 = pattern5.matcher(html);
html = matcher5.replaceAll(" ");
return html; 
}

/** 生成序号 */
public static String getRedPagerNo(String str) {
String no = Utility.formatDate(new Date());
no = no.replaceAll("-", "");
no = no.replaceAll(":", "");
no = no.replaceAll(" ", "");
String sRand = "";
for (int i = 0; i < 5; i++) {
String rand = String.valueOf(randoms.nextInt(10));
sRand += rand;
}
return str + no + sRand;
}

/**
* 把汉字转换为拼音

* @param input
* @return
*/
public static String toPinyin(String input) {
StringBuilder sb = new StringBuilder();
HanyuPinyinOutputFormat PINYIN_FORMAT = new HanyuPinyinOutputFormat();
PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_V);
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c <= 255) {
sb.append(c);
} else {
String pinyin = null;
try {
String[] pinyinArray = PinyinHelper
.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
pinyin = pinyinArray;
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
} catch (NullPointerException e) {
// 如果是日文,可能抛出该异常
e.printStackTrace();
}
if (pinyin != null) {
sb.append(pinyin);
sb.append(" ");
}
}
}
return sb.toString();
}



public static Long dateToLong(Date date) {
String strDate = formatDate(date);
strDate = strDate.replaceAll("-", "");
strDate = strDate.replaceAll(":", "");
strDate = strDate.replaceAll(" ", "");
return Long.valueOf(strDate);
}

public static void main(String[] arg) {
System.out.println(Utility.wipescript("<script>var d = 0;</script>"));
}
}
页: [1]
查看完整版本: 常用类型转换