总结了一些工作上常用到的方法,贴上来与大家共享。
/**
* <p>Title: 通用方法类</p>
* <p>Description: 常用的字符串及数字处理方法</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: joyinter.com</p>
* @author not attributable
* @version 1.0
*/
import java.text.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
public class CommonUtil {
private static DecimalFormat df = new DecimalFormat("#############0.00");
public static DecimalFormat getDecimalFormat()
{
return df;
}
/*******************
* 替换SQL语句中的'
* @param s
* @return
*/
public static String quote(String s) {
return "'" + s.replaceAll("'", "''") + "'";
}
/*********************
* 格式化日期
* @param pattern 日期格式
* @param date 日期对象
* @return
*/
public static String dateFormat(String pattern, java.util.Date date) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
return simpledateformat.format(date);
}
/*********************
* 无异常的parseInt方法
* @param s
* @return
*/
public static int parseInt(String s) {
if (s == null) {
return 0;
}
try {
return Integer.parseInt(s);
}
catch (NumberFormatException nfe) {
return 0;
}
}
/*********************
* 无异常的parseLong方法
* @param s
* @return
*/
public static long parseLong(String s) {
if (s == null) {
return 0;
}
try {
return Long.parseLong(s);
}
catch (NumberFormatException nfe) {
return 0;
}
}
/*********************
* 无异常的parseFloat方法
* @param s
* @return
*/
public static float parseFloat(String s) {
if (s == null) {
return 0;
}
try {
return Float.parseFloat(s);
}
catch (NumberFormatException nfe) {
return 0;
}
}
/*********************
* 回车符转换为<br>
* @param text
* @return
*/
public static String nb2br(String text) {
return text.replaceAll("\n", "<br>");
}
/*********************
* 根据List对象生成<select>控件
* @param l list
* @param formFieldName form中的名称
* @param defaultValue 默认值
* @return
*/
public static String genSelect(List l, String formFieldName,
Object defaultValue) {
StringBuffer buf = new StringBuffer();
buf.append("<select name=\"" + formFieldName + "\">");
if (formFieldName.equals("parentid")) {
buf.append("<option><根></option>");
}
for (int i = 0; i < l.size(); i++) {
boolean selected = false;
HashMap row = (HashMap) l.get(i);
Object key = row.keySet().iterator().next();
Object value = row.get(key);
if (key.equals(defaultValue)) {
selected = true;
}
buf.append("<option value=\"" + key + "\"");
if (selected) {
buf.append(" selected");
}
buf.append(">" + value + "</option>");
}
buf.append("</select>");
return buf.toString();
}
/*********************
* 将首字母改成大写
* @param str
* @return
*/
public static String uppercaseFirstChar(String str) {
str = str.substring(0, 1).toUpperCase() + str.substring(1);
return str;
}
/*********************
* 替换html中的特殊字符
* @param text
* @return
*/
public static String htmlSpecialChars(String text) {
if (text != null) {
text = text.replaceAll("&", "&");
text = text.replaceAll("\"", """);
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
}
return text;
}
/*********************
* 替换字符串中的\
* @param text
* @return
*/
public static String addSlash(String text) {
if (text != null) {
text = text.replaceAll("\\\"", "\\\\\"")
.replaceAll("\\\r\\\n", "\\\\r\\\\n");
}
return text;
}
/*********************
* 将回车转换为<br>
* @param text
* @return
*/
public static String nl2br(String text) {
return text.replaceAll("\r\n", "<br>");
}
/*********************
* 根据毫秒数得到中文时间
* @param millisecond
* @return
*/
public static String second2CNdate(double millisecond){
double second=Math.round(millisecond/1000);
int year,month,day,hour,minute;
String CNdate="";
if (second<0) return "无";
boolean flag=false;
year=(int)Math.floor(second/31104000);
if (year>0){
CNdate=year+"年 ";
flag=true;
second-=year*31104000;
}
month=(int)Math.floor(second/2592000);
if (month>0||flag){
CNdate+=month+"月 ";
flag=true;
second-=month*2592000;
}
day=(int)Math.floor(second/86400);
if (day>0||flag){
CNdate+=day+"天 ";
flag=true;
second-=day*86400;
}
hour=(int)Math.floor(second/3600);
if (hour>0||flag){
CNdate+=hour+"小时 ";
flag=true;
second-=hour*3600;
}
minute=(int)Math.floor(second/60);
if (minute>0||flag){
CNdate+=minute+"分 ";
flag=true;
second-=minute*60;
}
CNdate+=(int)second+"秒";
return CNdate;
}
public static String getRelativePath(String absPath, HttpServletRequest request){
String host = "http://"+request.getServerName();
if(request.getServerPort() != 80){
host += ":" + request.getServerPort();
}
return absPath.replaceAll(host, "");
}
public static String getDefinedLengthString(String str, int length, String addtion){
if(str.length()<length){
return str;
}else{
return str.substring(0, length)+addtion;
}
}
public static float percentFormat(int i, int sum){
if(sum==0){
return 100;
}else{
return (float)((i*10000/sum)/100);
}
}
public static String join(String[] arr, String seed){
if(arr == null) return null;
StringBuffer buf = new StringBuffer();
for(int i=0; i<arr.length; i++){
buf.append(arr[i]);
if(i!=arr.length-1){
buf.append(seed);
}
}
return buf.toString();
}
/**
* check if the operating system is a windows system, otherwise, we suggest it as linux system
* @return boolean
*/
public static boolean isWindowsOS(){
String osName = System.getProperty("os.name");
if(osName.indexOf(" ")>=0){
osName = osName.substring(0, osName.indexOf(" "));
}
if(osName.equalsIgnoreCase("WINDOWS")){
return true;
}else{
return false;
}
}
/**
* 生成hash code
* @param s String
* @return int
*/
public static long hCode(String s) {
long hash = 0;
for (int i = 0; i < s.length(); i++){
hash = (31 * hash) + s.charAt(i);
if((""+hash).length()>=5){
hash = parseLong((""+hash).substring((""+hash).length()-4));
}
}
return hash;
}
public static String getJs(String s){
StringBuffer buffer = new StringBuffer();
buffer.append("<script>");
buffer.append(s);
buffer.append("</script>");
return buffer.toString();
}
public static String getJsLog(int type, String msgName, String msgContent){
return getJs("window.parent.info.push("+type+", \""+msgName+"\", \""+msgContent+"\", new Date());");
}
}