java常用工具类

import java.text.*;
import java.util.*;

/**
*
* <p>
* Title: 通用工具类
* </p>
* <p>
* Description: 常用工具的集合,用来处理常见问题,比如中文乱码的方法等。
* </p>
* <p>
* Copyright: Copyright (c) 2003
* </p>
* <p>
* Company: Towery
* </p>
*
* @author WangPinHeng
* @version 1.0
*/
public class Tools {
public Tools() {
//
}

     /**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source
*             源字符串
* @param oldString
*             老的字符串
* @param newString
*             新的字符串
* @return 替换后的字符串
*/
private static String Replace(String source, String oldString,
String newString) {
StringBuffer output = new StringBuffer();

         int lengthOfSource = source.length(); // 源字符串长度
int lengthOfOld = oldString.length(); // 老字符串长度

         int posStart = 0; // 开始搜索位置
int pos; // 搜索到老字符串的位置

         while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));

             output.append(newString);
posStart = pos + lengthOfOld;
}

         if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
}

         return output.toString();
}

     /**
* 转换SQL中的特殊符号,比如将单引号"'"替换为"''",以免产生SQLException
*
* @param sqlstr
*             原SQL
* @return 处理后的SQL
*/
public static String toSql(String sqlstr) {
String strsql = sqlstr;
if (strsql == null) {
return "";
}
strsql = Replace(strsql, "'", "''");
return strsql;
}

     /**
* 将ISO8859_1编码的字符串转化为GB2312编码的字符串,主要用来处理中文显示乱码的问题
*
* @param ISO8859_1str
*             通过ISO8859_1编码的字符串
* @return 通过GB2312编码的字符串
*/
public String GBString(String ISO8859_1str) {
if (ISO8859_1str == null) {
return "";
} else {
try {
return new String(ISO8859_1str.getBytes("ISO8859_1"), "GB2312");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

     /**
* 将ISO8859_1编码的字符串转化为GB2312编码的字符串,主要用来处理中文显示乱码的问题
*
* @param ISO8859_1str
*             通过ISO8859_1编码的字符串
* @return 通过GB2312编码的字符串
*/
public static String GB2312FromISO8859_1(String ISO8859_1str) {
if (ISO8859_1str == null) {
return "";
} else {
try {
return new String(ISO8859_1str.getBytes("ISO8859_1"), "GB2312");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

     public String trim1(String str) {
String str1 = "";

         if (str.substring(0, 1).equals("?")) {
str1 = str.substring(1, str.length());
System.out.println("str1=" + str1);
} else {
str1 = str;
}

         return str1.trim();
}

     public String dateformat(String str) {
String day = str.substring(0, 2);
String month = str.substring(3, 5);
String year = str.substring(6, 10);
str = year + "-" + month + "-" + day;
return str;
}

     public String tenformat(String str) {
if (str.length() == 8) {
String year = str.substring(0, 4);
String month = "0" + str.substring(5, 6);
String day = "0" + str.substring(7, 8);
str = year + "-" + month + "-" + day;
} else if (str.length() == 9) {
if (str.substring(6, 7).equals("-")) {
str = str.substring(0, 5) + "0" + str.substring(5, 9);
} else {
str = str.substring(0, 8) + "0" + str.substring(8, 9);
}

         }
return str;
}

     /**
* 计算两个时间
*
* @param str
*             原时间,strsub,需减少的时间
* @return 计算后的时间
*/
public String subTime(String str, String strSub) {
String rsTime = "";
int hour = 0;
int sec = 0;
int secsub = 0;
str = trim(str);
strSub = trim(strSub);
if (str.length() == 5) {
hour = Integer.parseInt(str.substring(0, 2));

             sec = Integer.parseInt(str.substring(3, 5));

         } else if (str.length() == 4) {
hour = Integer.parseInt(str.substring(0, 1));

             sec = Integer.parseInt(str.substring(2, 4));

         }
if (trim(strSub).length() == 5) {
secsub = Integer.parseInt(strSub.substring(0, 2));

         } else if (trim(strSub).length() == 4) {
secsub = Integer.parseInt(strSub.substring(0, 1));

         }

         //int a = sec + secsub;
if (sec < secsub) {
//System.out.println("sub <");
String jstr = Integer.toString(sec + 60 - secsub);
String hstr = Integer.toString(hour - 1);
//System.out.println("jstr="+jstr);
//System.out.println("hstr="+hstr);
if (jstr.length() == 1) {
jstr = "0" + jstr;
}
if (hstr.length() == 1) {
hstr = "0" + hstr;
}
rsTime = hstr + ":" + jstr;

         } else if (sec == secsub) {
//System.out.println("sub =");
String strh = Integer.toString(hour);
//System.out.println("strh="+strh);
if (strh.length() == 1) {
strh = "0" + strh;
}
rsTime = strh + ":00";

         } else if (sec > secsub) {
//System.out.println("sub >");
String jstr = Integer.toString(sec - secsub);
//System.out.println("jstr="+jstr);
String hstr = Integer.toString(hour);
//System.out.println("hstr="+hstr);
if (jstr.length() == 1) {
jstr = "0" + jstr;
}
if (hstr.length() == 1) {
hstr = "0" + hstr;
}
rsTime = hstr + ":" + jstr;

         }
return rsTime;
}

     public String toSENDstr(String input) {
String r = input;
r = replace(r, "&", "");
r = replace(r, "/", "|");
r = replace(r, "\r", "");
r = replace(r, "\n", "");
r = replace(r, "'", "");
r = replace(r, " ", "");
return r;
}

     public String replace(String str, String strOld, String strNew) {
String r = str;
if (str != null && strOld != null && strNew != null) {
int idx = str.indexOf(strOld);
if (idx != -1) {
String strPre = "";
r = "";
String strSuf = str;
for (; idx != -1; idx = strSuf.indexOf(strOld)) {
strPre = strSuf.substring(0, idx);
strSuf = strSuf.substring(idx + strOld.length());
r = r + strPre + strNew;
}

                 r = r + strSuf;
}
}
return r;
}

     /**
* 计算两个时间相差的分钟数
*
* @param time1
*             string,time2,string
* @return string
*/
public String diffTime(String time1, String time2) {
String rsTime = "";
int hour = 0;
int hour2 = 0;
int sec = 0;
int sec2 = 0;
String str1 = trim(time1);
String str2 = trim(time2);
if (str1.length() == 5) {
hour = Integer.parseInt(str1.substring(0, 2));

             sec = Integer.parseInt(str1.substring(3, 5));

         } else if (str1.length() == 4) {
hour = Integer.parseInt(str1.substring(0, 1));

             sec = Integer.parseInt(str1.substring(2, 4));

         }
if (str2.length() == 5) {
hour2 = Integer.parseInt(str2.substring(0, 2));

             sec2 = Integer.parseInt(str2.substring(3, 5));

         } else if (str2.length() == 4) {
hour2 = Integer.parseInt(str2.substring(0, 1));

             sec2 = Integer.parseInt(str2.substring(2, 4));

         }

         //int a = sec + secsub;
if (sec < sec2) {
//System.out.println("sub <");
String jstr = Integer.toString(sec + 60 - sec2);
if (jstr.length() == 1) {
jstr = "0" + jstr;
}
if ((hour - 1) != hour2) {

                 String hstr = Integer.toString(hour - 1 - hour2);

                 if (hstr.length() == 1) {
hstr = "0" + hstr;
}
rsTime = hstr + ":" + jstr + ":00";
} else {
rsTime = jstr + ":00";
}
} else if (sec == sec2) {
//System.out.println("sub =");
if (hour != hour2) {

                 String strh = Integer.toString(hour - hour2);
//System.out.println("strh="+strh);
if (strh.length() == 1) {
strh = "0" + strh;
}
rsTime = strh + ":00" + ":00";
} else {
rsTime = "00:00";
}
} else if (sec > sec2) {
//System.out.println("sub >");
String jstr = Integer.toString(sec - sec2);
//System.out.println("jstr="+jstr);
if (jstr.length() == 1) {
jstr = "0" + jstr;
}
if (hour != hour2) {
String hstr = Integer.toString(hour - hour2);
//System.out.println("hstr="+hstr);
if (hstr.length() == 1) {
hstr = "0" + hstr;
}
rsTime = hstr + ":" + jstr + ":00";
} else {
rsTime = jstr + ":00";
}
}
return rsTime;
}

     /**
* 计算两个时间
*
* @param str
*             原时间,stradd,需增加的时间
* @return 计算后的时间
*/
public String addTime(String str, String stradd) {
String rsTime = "";
int hour = 0;
int sec = 0;
int secadd = 0;
int houradd = 0;
str = trim(str);
stradd = trim(stradd);
if (str.length() == 5) {
hour = Integer.parseInt(str.substring(0, 2));

             sec = Integer.parseInt(str.substring(3, 5));

         } else if (str.length() == 4) {
hour = Integer.parseInt(str.substring(0, 1));

             sec = Integer.parseInt(str.substring(2, 4));

         }
if (trim(stradd).length() == 5) {

             secadd = Integer.parseInt(stradd.substring(0, 2));

         } else if (trim(stradd).length() == 4) {
secadd = Integer.parseInt(stradd.substring(0, 1));

         } else if (trim(stradd).length() == 7) {
houradd = Integer.parseInt(stradd.substring(0, 1));
secadd = Integer.parseInt(stradd.substring(2, 4));
}
int a = sec + secadd;
if (a < 60) {
String stra = Integer.toString(a);
String strh = Integer.toString(hour + houradd);
if (stra.length() == 1) {
stra = "0" + stra;
}
if (strh.length() == 1) {
strh = "0" + strh;
} else if (Integer.parseInt(strh) > 24) {
int h = Integer.parseInt(strh) / 24;
strh = Integer.toString(h);
if (h < 10) {
strh = "0" + Integer.toString(h);
}
}
rsTime = strh + ":" + stra;

         } else if (a == 60) {
String strh = Integer.toString(hour + houradd + 1);
if (strh.length() == 1) {
strh = "0" + strh;
} else if (Integer.parseInt(strh) > 24) {
int h = Integer.parseInt(strh) / 24;
strh = Integer.toString(h);
if (h < 10) {
strh = "0" + Integer.toString(h);
}
}
rsTime = strh + ":00";

         } else if (a > 60) {
int i = a / 60;
int j = a % 60;
String strj = Integer.toString(j);

             if (strj.length() == 1) {
strj = "0" + strj;
}
String strh = Integer.toString(hour + houradd + i);
if (strh.length() == 1) {
strh = "0" + strh;
} else if (Integer.parseInt(strh) > 24) {
int h = Integer.parseInt(strh) / 24;
strh = Integer.toString(h);
if (h < 10) {
strh = "0" + Integer.toString(h);
}
}
rsTime = strh + ":" + strj;

             if (j == 0) {
rsTime = strh + ":00";

             }

         }
return rsTime;
}

你可能感兴趣的:(java,sql,J#)