import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.text.*;
import java.util.*;
/**
* <p>Title: 常用方法类</p>
* <p>Description:提供 字符串替换 字符串分割 将ISO-8859-1编码转换为GB2312
* 处理空字符串 处理空对象 将字符串转化为整数 断是否为整数 字符串转换为java.util.Date
* 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)
* 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)
* 取系统当前时间:返回只值为如下形式2002-10-30 20:24:39
* 取系统当前时间:返回只值为如下形式2002-10-30 08:28:56 下午
* 取系统当前时间:返回只值为如下形式2002-10-30</p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
/**
* 增加了判断对象所属类型的方法。例如:要测试某一对象obj是否是Map类型,
* 调用此类中的CheckMap(obj)方法,若为Map对象,则返回True,否则返回False。
*/
public class cCharControl {
/**
*字符串替换
*@param strFrom 要替换的目标子串
*@param strTo 替换后的子串
*@param strSource 原字符串
*@return 替换后的字符串
*/
public static String replace(String strSource, String strFrom, String strTo) {
String strDest = "";
int intFromLen = strFrom.length();
int intPos;
while ((intPos = strSource.indexOf(strFrom)) != -1) {
strDest = strDest + strSource.substring(0, intPos);
strDest = strDest + strTo;
strSource = strSource.substring(intPos + intFromLen);
}
strDest = strDest + strSource;
return strDest;
}
/**
*计算一个字符串中的子串数量,
*/
public static int childStrNo(String str, String delim) {
int cur = 0;
int numno = 1;
do {
cur = str.indexOf(delim);
if (cur == -1) {
break;
}
str = str.substring(cur + 1, str.length());
numno = numno + 1;
} while (true);
return numno;
}
/**
*字符串分割
* @param str 原字符串
* @param delim 分割子串
* @return String[]
*/
public static String[] split(String str, String delim) {
int last = 0;
int cur = 0;
String[] result = null;
ArrayList holder = new ArrayList();
do {
String tmp;
cur = str.indexOf(delim, cur);
if (cur == -1) {
tmp = str.substring(last);
holder.add(tmp);
break;
}
tmp = str.substring(last, cur);
holder.add(tmp);
cur += delim.length();
last = cur;
} while (true);
result = new String[holder.size()];
for (int i = 0; i < holder.size(); i++) {
result[i] = (String) holder.get(i);
}
return result;
}
/**
* 将ISO-8859-1编码转换为 GB2312
* @param str 目标字符串
* @return String
*/
public static String ISOtoGb2312(String str) {
if (str == null) {
return " ";
}
try {
byte[] bytesStr = str.getBytes("ISO-8859-1");
return new String(bytesStr, "GBK");
} catch (Exception ex) {
return str;
}
}
public static String Gb2312toISO(String str) {
if (str == null) {
return " ";
}
try {
byte[] bytesStr = str.getBytes("GBK");
return new String(bytesStr, "ISO-8859-1");
} catch (Exception ex) {
return str;
}
}
/**
* 处理空字符串
* @param str要处理的字符串 如果str为null返回""
* @return String 如果str为null返回"" 否则返回str
*/
public static String dealNull(String str) {
String returnstr = null;
if (str == null) {
returnstr = "";
} else {
returnstr = str;
}
return returnstr;
}
/**
*处理空对象
*将null转化为""对象
* @param obj:Object
* @return Object
*/
public static Object dealNull(Object obj) {
Object returnstr = null;
if (obj == null) {
returnstr = (Object) ("");
} else {
returnstr = obj;
}
return returnstr;
}
/**
*将字符串转化为整数
*如果参数为null或“”或不能转换为整数则返回0
*如“阿不错23”返回结果为0
*/
public static int intToString(String s) {
s = dealNull(s);
int x = 0;
if (s.equals("")) {
return 0;
}
try {
x = Integer.parseInt(s);
} catch (Exception e) {}
return x;
}
/**
*断是否为数字整数
* @param number
* @return boolean:true or false
*/
public static boolean isInt(String number) {
try {
Integer.parseInt(number);
return true;
} catch (NumberFormatException sqo) {
return false;
}
}
/**
* 字符串转换为java.util.Date<br>
* 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br>
* yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>
* yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm'<br>
* yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' <br>
* yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' <br>
* @param time String 字符串<br>
* @return Date 日期<br>
*/
public static Date stringToDate(String time) {
SimpleDateFormat formatter;
int tempPos = time.indexOf("AD");
time = time.trim();
formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
if (tempPos > -1) {
time = time.substring(0, tempPos) +
"公元" + time.substring(tempPos + "AD".length()); //china
formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
}
tempPos = time.indexOf("-");
if (tempPos > -1 && (time.indexOf(" ") < 0)) {
formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");
} else if ((time.indexOf("/") > -1) && (time.indexOf(" ") > -1)) {
formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
} else if ((time.indexOf("-") > -1) && (time.indexOf(" ") > -1)) {
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} else if ((time.indexOf("/") > -1) && (time.indexOf("am") > -1) ||
(time.indexOf("pm") > -1)) {
formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
} else if ((time.indexOf("-") > -1) && (time.indexOf("am") > -1) ||
(time.indexOf("pm") > -1)) {
formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
}
ParsePosition pos = new ParsePosition(0);
java.util.Date ctime = formatter.parse(time, pos);
return ctime;
}
/**
* 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
* 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
* @param time Date 日期<br>
* @return String 字符串<br>
*/
public static String dateToString(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ctime = formatter.format(time);
return ctime;
}
/**
* 将java.util.Date 格式转换为字符串格式'yyyyMMddHHmmss'(24小时制)<br>
* 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
* @param time Date 日期<br>
* @return String 字符串<br>
* add by cye 2003-8-20
*/
public static String dateToString11(Date time) {
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String