import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectSybase {
public static void main(String []args) throws Exception{
Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
// con=os.getConnection();
System.out.println("OK");
Connection co = DriverManager.getConnection(
"jdbc:sybase:Tds:10.17.34.188:5000/spms", "sa","");
if (co != null) {
System.out.println(co);
}
}
}
/**
* <p>Title:Tool </p>
* <p>Description:工具类,常用的方法 </p>
* <p>Copyright:(c) </p>
* <p>Company: eshore </p>
* @author lik
* @version 1.0
*/
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.DataFormatException;
import javax.servlet.http.HttpServletRequest;
import sun.io.ByteToCharConverter;
import sun.io.CharToByteConverter;
public class Tool {
public Tool() {
}
/**
* 将源对象中与目标对象中相等的字段的值复制到目标对象中
* @param pSource Object 源对象
* @param pTarget Object 目标对象
*/
public static void copyValue(Object pSource, Object pTarget) {
Object tReturn = null;
try {
//获得所有的属性
Field[] fields = pSource.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field fiel = fields[i];
String name = fiel.getName();
String tName = null;
try {
pTarget.getClass().getDeclaredField(name);
}
catch (Exception ex) {
Debug.print(ex);
continue;
}
Debug.println("method name is " + name);
Class type = fiel.getType();
String setMethodName = "set" + name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
String getMethodName = "get" + name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Class[] param = {
type};
Debug.println("returntype is" + type.getName() + "ff " + getMethodName);
Method setMethoded = null;
try {
setMethoded =
pTarget.getClass().getDeclaredMethod(setMethodName, param);
Debug.println("------method is" + setMethoded.getName());
Method getMethoded = null;
getMethoded =
pSource.getClass().getDeclaredMethod(getMethodName, null);
Debug.println("====method is" + getMethoded.getName());
Object[] params = {};
tReturn = getMethoded.invoke(pSource, params);
Object[] paramReturn = {
tReturn};
setMethoded.invoke(pTarget, paramReturn);
}
catch (Exception ex) {
Debug.println("de get method catch" + ex);
continue;
}
} //end for
}
catch (Exception ex) {
Debug.println("insert catch" + ex);
}
}
/**
* 该函数将在指定的字符前插入一个字符
* @param pSource String 要替换的字符串
* @param pOb char 指定的字符
* @param pAcc char 要插入的字符
* @return String 处理好的字符串
*/
public static String tranString(String pSource, char pOb, char pAcc) {
if (pSource == null || pSource.equals(""))
return "";
String strTmp = "";
int flag = pSource.indexOf(pOb);
String strM = new String(pSource);
if (flag != -1) {
while (true) {
if (flag > 0) {
strTmp = strTmp + strM.substring(0, flag) + pAcc + pOb;
}
else {
strTmp = strTmp + pAcc + pOb;
}
if (flag >= strM.length()) {
break;
}
else {
strM = strM.substring(flag + 1);
flag = strM.indexOf(pOb);
if (flag == -1) {
break;
}
}
}
return strTmp;
}
else {
return pSource;
}
}
/**
* 该函数将在指定的字符串中特定替换成新的字符串
* @param rStr String 指定字符串
* @param rFix String 要替换的字符串
* @param rRep String 要替换成的字符串
* @return String 已替换的字符串
*/
public static String StrReplace(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
int i = 0;
if ( (i = line.indexOf(oldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ( (i = line.indexOf(oldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
public static String replaceIgnoreCase(String line, String oldString,
String newString) {
if (line == null) {
return null;
}
String lcLine = line.toLowerCase();
String lcOldString = oldString.toLowerCase();
int i = 0;
if ( (i = lcLine.indexOf(lcOldString, i)) >= 0) {
char[] line2 = line.toCharArray();
char[] newString2 = newString.toCharArray();
int oLength = oldString.length();
StringBuffer buf = new StringBuffer(line2.length);
buf.append(line2, 0, i).append(newString2);
i += oLength;
int j = i;
while ( (i = lcLine.indexOf(lcOldString, i)) > 0) {
buf.append(line2, j, i - j).append(newString2);
i += oLength;
j = i;
}
buf.append(line2, j, line2.length - j);
return buf.toString();
}
return line;
}
/**
* 从request中封装一个对象
* @param pRequest HttpServletRequest request对象
* @param pModel Class 封装对象的类
* @return Object 超类
* @throws Exception
*/
public static Object getParameter(HttpServletRequest pRequest, Class pModel) throws
Exception {
Object tReturn = null;
tReturn = pModel.newInstance();
//获得所有的属性
Field[] fields = pModel.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
Field fiel = fields[i];
String name = fiel.getName();
Class type = fiel.getType();
String methodName = "set" + name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Debug.println("方法名:" + methodName);
Object returnValue = null;
Object[] paras = {};
Class[] para = {
type};
Method methoded = pModel.getDeclaredMethod(methodName, para);
Class[] returnTypes = methoded.getParameterTypes();
Class returnType = returnTypes[0];
returnValue = pRequest.getParameter(name);
if (returnType == java.lang.String.class) {
if (returnValue != null) {
returnValue = ( (String) returnValue).trim();
}
}
else if ( (returnType == int.class) || (returnType == Integer.class)) {
if (returnValue != null) {
try {
returnValue = new Integer(Integer.parseInt( (String)
returnValue));
}
catch (NumberFormatException ex1) {
returnValue = new Integer(0);
}
}
}
else if ( (returnType == double.class) || (returnType == Double.class)) {
if (returnValue != null) {
returnValue = new Double(Double.parseDouble( (String) returnValue));
}
}
else if (returnType == java.util.Collection.class) {
String[] pars = pRequest.getParameterValues(name);
if (pars != null && pars.length > 0) {
ArrayList li = new ArrayList();
for (int j = 0; j < pars.length; j++) {
li.add(pars[j]);
Debug.println("id is " + pars[j]);
}
returnValue = li;
}
}
else if ( (returnType == float.class) || (returnType == Float.class)) {
if (returnValue != null) {
returnValue = new Float(Float.parseFloat( (String) returnValue));
}
}
else if (returnType == java.util.Date.class) {
if (returnValue != null) {
try {
SimpleDateFormat formatter
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTmp = (String) returnValue;
if (dateTmp.length() < 11) {
returnValue = dateTmp + " 00:00:00";
}
returnValue = formatter.parse( (String) returnValue);
}
catch (Exception ex) {
Debug.println(ex);
}
}
}
else if (returnType == java.sql.Date.class) {
if (returnValue != null) {
try {
DateFormat df = DateFormat.getDateInstance();
returnValue
= new java.sql.Date(df.parse( (String) returnValue).getTime());
}
catch (Exception ex) {
Debug.print(ex);
}
}
}
else {
Debug.println("没有处理的类型 " + returnType.getName());
}
Object[] params = {
returnValue};
methoded.invoke(tReturn, params);
}
catch (Exception ex) {
ex.printStackTrace();
Debug.println("invoke catch:" + ex.getMessage());
}
}
return tReturn;
}
/**
* 将ISO8859_1字符转为gb2312编码
* @param pStr String 要进行转码的字符
* @return String 转码后的字符
*/
public static String convertEncoding(String pStr) {
String target = pStr;
if (target != null) {
try {
target = new String(pStr.getBytes("8859_1"), "gb2312");
}
catch (Exception e) {
Debug.println("转码失败!");
}
}
return target;
}
/**
* 将ISO8859_1字符转为gb2312编码
* @param pStr String 要进行转码的字符
* @return String 转码后的字符
*/
public static String unConvertEncoding(String pStr) {
String target = pStr;
try {
target = new String(pStr.getBytes("gb2312"), "8859_1");
}
catch (Exception e) {
Debug.println("转码失败!");
}
return target;
}
/**
* 按时间得到随机数
* @return string 随机数
*/
public static String getRandom() {
java.util.Date date = new java.util.Date();
return (date.getTime() + "").substring(2);
}
/**
* 此方法为一种对回车换行的翻译为相关代码值的方法,用<pre></pre>可以搞定
* @param string String 要处理的字符串
* @return String 处理后的字符串
*/
public static String translateString(String string) {
String returnStr = "";
int begin = 0;
char c[] = {
'\r', '\n', '\r', '\n', '\r', '\n'};
String str = new String(c);
int index = string.indexOf(str);
String sub = new String();
if (index != -1)
sub = string.substring(index + 6, string.length());
else
sub = string;
index = 0;
Vector vc = new Vector();
char c1[] = {
'\r', '\n'};
String str1 = new String(c1);
index = sub.indexOf(str1);
while (index != -1) {
String s = sub.substring(0, index);
vc.addElement(s);
sub = sub.substring(index + 1, sub.length());
index = sub.indexOf(str1);
}
String s = sub.substring(begin);
vc.addElement(s);
if (!vc.isEmpty()) {
int count1 = vc.size();
for (int k = 0; k < count1; k++)
returnStr = returnStr + (String) vc.elementAt(k) + "<br>";
}
return returnStr;
}
/**
* 截取字符串,可以处理中,英的问题
* @param sourceString String 待处理的字符串
* @param length int 截取的长度,是字节长度,一个中文两个字节
* @return String 返回截取后的字符串
*/
public static String substring(String sourceString, int length) {
char[] sourceChrs = sourceString.toCharArray();
char[] distinChrs = new char[length];
for (int i = 0; i < length; i++) {
if (i >= sourceChrs.length) {
break;
}
Character chr = new Character(sourceChrs[i]);
if (chr.charValue() <= 202 && chr.charValue() >= 8) {
distinChrs[i] = chr.charValue();
}
else {
distinChrs[i] = chr.charValue();
length--;
}
}
return new String(distinChrs);
}
/**
* 判断字符串长度是否大于给定字节长度,可以处理中,英文的问题,
* ( 与上面的 substring 配合使用 )
* @param sourceString String 待比较的字符串
* @param length int 给定的字节长度,是字节长度,一个中文两个字节
* @return boolean 判断后的结果 如果大于返回true,否则返回 false
*/
public static boolean longThen(String sourceString, int length) {
int index = 0;
char[] sourceChrs = sourceString.toCharArray();
int sourceLength = sourceChrs.length;
for (int i = 0; i < sourceLength; i++) {
Character chr = new Character(sourceChrs[i]);
if (chr.charValue() <= 202 && chr.charValue() >= 8) {
index++;
}
else {
index += 2;
}
}
return index > length;
}
/**
* 截取字符串,可以处理中,英的问题 + "…"
* @param sourceString String 待处理的字符串
* @param length int 截取的长度,是字节长度,一个中文两个字节
* @return String 返回截取后的字符串
*/
public static String subAdd(String sourceString, int length) {
char[] sourceChrs = sourceString.toCharArray();
char[] distinChrs = new char[length];
for (int i = 0; i < length; i++) {
if (i >= sourceChrs.length) {
return sourceString;
}
Character chr = new Character(sourceChrs[i]);
if (chr.charValue() <= 202 && chr.charValue() >= 8) {
distinChrs[i] = chr.charValue();
}
else {
distinChrs[i] = chr.charValue();
length--;
}
}
return new String(distinChrs) + "…";
}
/**
* 字符串自动换行,可以处理中,英的问题
* @param sourceString String 待处理的字符串
* @param length int 每行的字节长度,是字节长度,一个中文两个字节
* @return String 返回经过换行后的字符串
*/
public static String autoNewLine(String sourceString, int length) {
String destinStr = "";
char[] sourceChrs = sourceString.toCharArray();
int sourceLeanth = sourceChrs.length;
int rowLeanth = 0;
int index = 0;
while (true) {
rowLeanth = length;
char[] distinChrs = new char[rowLeanth];
for (int i = 0; i < rowLeanth; i++) {
if (index >= sourceLeanth) {
break;
}
Character chr = new Character(sourceChrs[index]);
index++;
if (chr.charValue() <= 202 && chr.charValue() >= 8) {
distinChrs[i] = chr.charValue();
}
else {
distinChrs[i] = chr.charValue();
rowLeanth--;
}
}
destinStr = destinStr + (new String(distinChrs)) + "<br>";
if (index >= sourceLeanth) {
break;
}
}
return destinStr.substring(0, destinStr.length() - 4); //new String(distinChrs);
}
/**
* 得到字符串长度,可以处理中,英文的问题,
* @param sourceString String 待测量的字符串
* @return int 得到的字节长度,是字节长度,一个中文两个字节
* @author zhaoqs
*/
public static int getLength(String sourceString) {
int index = 0;
char[] sourceChrs = sourceString.toCharArray();
int sourceLength = sourceChrs.length;
for (int i = 0; i < sourceLength; i++) {
Character chr = new Character(sourceChrs[i]);
if (chr.charValue() <= 202 && chr.charValue() >= 8) {
index++;
}
else {
index += 2;
}
}
return index;
}
/**
* 格式化字符串,在字符串两端添加指定字符,已达到指定长度,比如:源串为“公司公告”
* length 为 22,formatChr 为 “-”,则得到字符串 -----公司公告----
* @param String sourceString 待格式的字符串
* @param int length 给定的字节长度,是字节长度,一个中文两个字节
* @return string 格式后的结果
* @author zhaoqs
*/
public static String formatString(String sourceString, int length,
String formatChr) {
int index = 0;
int sourceLen = getLength(sourceString);
int formatLen = getLength(formatChr);
int loopInt = (length - sourceLen) / (2 * formatLen);
String formatStr = "";
for (int i = 0; i < loopInt; i++) {
formatStr += formatChr;
}
return formatStr + sourceString + formatStr;
}
/*
public static int copyStream(InputStream in, OutputStream out) throws
IOException {
int result = 0;
byte[] buf = new byte[8 * 1024];
for (; ; ) {
int numRead = in.read(buf);
if (numRead == -1)
break;
out.write(buf, 0, numRead);
result += numRead;
}
out.flush();
return result;
}
public static byte[] getStreamBytes(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Tools.copyStream(in, out);
return out.toByteArray();
}
*/
/**
* 从输入流中封装一个byte[]
* @param stream InputStream 输入流
* @throws IOException
* @return byte[] byte数组
*/
public static byte[] getStreamBytes(InputStream stream) throws IOException {
BufferedInputStream bis = new BufferedInputStream(stream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
byte[] buffer = new byte[8 * 1024];
int readCount;
while ( (readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
bos.close();
bis.close();
stream.close();
byte[] tContent = baos.toByteArray();
baos.close();
baos = null;
bos = null;
stream = null;
bis = null;
return tContent;
}
/**
* 删除文件夹
* @param String filePath 待删除文件夹路径
* @throws Exception
*/
public static void delfileTree(String filePath) throws Exception {
java.io.File file = new java.io.File(filePath);
if (file.isDirectory()) { //删除子目录
String[] filelist = file.list(); //列出所有的子文件(夹)名字
for (int i = 0; i < filelist.length; i++) {
delfileTree(filePath + "\\" + filelist[i]);
//System.out.println(filelist[i]+"是树");
}
}
else { //删除文件
file.delete();
//System.out.println("删除"+file);
}
file.delete(); //删除该文件夹
}
/**
* 转换邮件中title的乱码,例如:
* (=?GB2312?Q?Fw: 2002=D1=D0=CC=D6=BB=E1=B6=AF=D4=B1=B4=F3=BB=E1=F4=DF=C7=
* E9=BF=F6=CD=A8=B1=A8=A3=A8=BB=E1=D2=E9=BC=C7=C2=BC=A3=A9?=)
* @param str 待转换字符串
* @return String 转换好的字符串
*/
public static String mailTitleConverter(String str) {
String tReturn = str;
StringBuffer temp = new StringBuffer().append(str);
if (str != null) {
int ii = 0;
//System.out.println("##############################");
Debug.println("原码:" + str);
String str2 = str.toUpperCase();
if ( ( (ii = str2.indexOf("=?GB2312?Q?")) != -1) ||
( (ii = str2.indexOf("=?GB2312?B?")) != -1) ||
( (ii = str2.indexOf("=?ISO-8859-1?Q?")) != -1) ||
( (ii = str2.indexOf("=?ISO-8859-1?B?")) != -1) ||
( (ii = str2.indexOf("=?BIG5?Q?")) != -1) ||
( (ii = str2.indexOf("=?BIG5?B?")) != -1) ||
( (ii = str2.indexOf("=?US-ASCII?Q?")) != -1) ||
( (ii = str2.indexOf("=?US-ASCII?B?")) != -1) ||
( (ii = str2.indexOf("=?UNICODE-1-1-UTF-7?Q?")) != -1) ||
( (ii = str2.indexOf("=?UNICODE-1-1-UTF-7?B?")) != -1)) {
str = null;
str = temp.toString();
str = Tool.replaceIgnoreCase(str, "=?us-ascii?Q?", "");
str = Tool.replaceIgnoreCase(str, "=?us-ascii?B?", "");
str = Tool.replaceIgnoreCase(str, "=?big5?Q?", "");
str = Tool.replaceIgnoreCase(str, "=?big5?B?", "");
str = Tool.replaceIgnoreCase(str, "=?ISO-8859-1?Q?", "");
str = Tool.replaceIgnoreCase(str, "=?ISO-8859-1?B?", "");
str = Tool.replaceIgnoreCase(str, "=?GB2312?Q?", "");
str = Tool.replaceIgnoreCase(str, "=?GB2312?B?", "");
str = Tool.replaceIgnoreCase(str, "=?UNICODE-1-1-UTF-7?Q?", "");
str = Tool.replaceIgnoreCase(str, "=?UNICODE-1-1-UTF-7?B?", "");
str = Tool.replaceIgnoreCase(str, "?=", "");
byte[] main = str.getBytes();
byte[] remain = new byte[main.length];
//System.out.println("截取后:"+new String(main));
//System.out.println("##############################");
int index = 0;
int i = 0;
while (index < main.length) {
if (main[index] == '=') {
index++;
byte a1 = main[index];
if (a1 >= 65) {
a1 -= 55;
}
else {
a1 -= 48;
}
index++;
byte a2 = main[index];
if (a2 >= 65) {
a2 -= 55;
}
else {
a2 -= 48;
}
remain[i] = (byte) (a1 * 16 + a2);
index++;
i++;
}
else {
remain[i] = main[index];
i++;
index++;
}
}
str = null;
str = new String(remain).substring(0, i);
}
}
return str;
}
/**
* 转码函数:String到UNICODE
* @param u String 待转字符串
* @return String 转好字符串
*/
public static String uCode(String u) {
int iLen = u.length();
String c;
String cStr = "";
int k;
if (iLen == 0)
return "";
for (int i = 0; i < iLen; i++) {
c = u.substring(i, i + 1);
k = c.hashCode();
if (k > 255)
cStr = cStr + String.valueOf( (char) (k >> 8)) +
String.valueOf( (char) (k & 255));
else
cStr = cStr + String.valueOf( (char) 0) + c;
}
return cStr;
}
/**
* 转码函数:UNICODE到CHAR
* @param str String 待转字符串
* @return String 转好字符串
*/
public static String uDCode(String str) {
int iLen = str.length();
if (iLen % 2 != 0)
return "-1";
String Out = "";
for (int i = 0; i < iLen / 2; i++)
Out = Out +
String.valueOf( (char) ( ( ( (int) str.charAt(i * 2)) << 8) +
(int) str.charAt(i * 2 + 1)));
return Out;
}
/**
* 内部加密函数
* @param str String 待加密的字符串
* @return String 已加密的字符串
*/
public static String encode(String str) {
if (str == null)
return null;
int iLen = str.length();
if (iLen == 0)
return "";
str = uCode(str);
iLen = str.length();
String tempStr1, tempStr2, tempStr3;
String tempOut = "";
String Out = "";
Random r = new Random();
int l = 0;
for (int n = 0; n < iLen; n++) {
tempStr1 = Integer.toHexString( (str.substring(n, n + 1).hashCode()) >> 4);
tempStr2 = Integer.toHexString( (str.substring(n, n + 1).hashCode()) & 15);
tempStr3 = Integer.toString(r.nextInt());
tempStr3 = tempStr3.substring(tempStr3.length() - 1, tempStr3.length());
if (n % 3 == 0)
tempOut = tempOut + tempStr3 + tempStr1 + tempStr2;
else if (n % 3 == 1)
tempOut = tempOut + tempStr1 + tempStr3 + tempStr2;
else
tempOut = tempOut + tempStr1 + tempStr2 + tempStr3;
}
return tempOut;
}
/**
* 内部解密的函数
* @param str String 待解密的字符串
* @return String 已解密的字符串
*/
public static String decode(String str) {
if (str == null)
return null;
int iLen = str.length();
if (iLen == 0)
return ""; //密码为空
String tStr = ""; //临时字符串
String hexStr = ""; //16进制字符串
String Out = "";
hexStr = str;
if (hexStr.length() % 3 != 0)
return "-1";
for (int i = 0; i < hexStr.length() / 3; i++)
if (i % 3 == 0)
tStr = tStr + hexStr.substring(i * 3 + 1, (i + 1) * 3);
else if (i % 3 == 1)
tStr = tStr + hexStr.substring(i * 3, i * 3 + 1) +
hexStr.substring(i * 3 + 2, (i + 1) * 3);
else
tStr = tStr + hexStr.substring(i * 3, i * 3 + 2);
for (int i = 0; i < tStr.length() / 2; i++)
Out = Out +
String.valueOf( (char) ( ( (Char2int(tStr.charAt(i * 2))) << 4) +
Char2int(tStr.charAt(i * 2 + 1))));
Out = uDCode(Out);
return Out;
}
/**
* 16进制字符转换为10进制数值
* @param s char 代转字符
* @return int 转换后10进制数值
*/
public static int Char2int(char s) {
if (s >= '0' && s <= '9') {
return s - '0';
}
else if (s >= 'a' && s <= 'f') {
return s - 'a' + 10;
}
else {
return -1;
}
}
/**
* 根据","分隔符来取列表
* @param str String 待取字符串
* @return List 取得的字符串列表
*/
public static List stringParseToList(String str) {
List tempArrList = new ArrayList();
StringTokenizer st = new java.util.StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
tempArrList.add(st.nextToken());
}
return tempArrList;
}
/**
* 根据传入参数分隔符来取列表
* @param str String 待取字符串
* @param sign String 分隔符
* @return List 取得的字符串列表
*/
public static List stringParseToList(String str, String sign) {
List tempArrList = new ArrayList();
StringTokenizer st = new java.util.StringTokenizer(str, sign);
while (st.hasMoreTokens()) {
tempArrList.add(st.nextToken());
}
return tempArrList;
}
/**
* 根据 ",","."分隔符来取List二维列表
* @param str String 待取字符串
* @return List 取得的字符串列表
*/
public static List _stringParseToList(String str) {
List tempArrList = new ArrayList();
StringTokenizer st = new java.util.StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
String strvalue = st.nextToken();
StringTokenizer st1 = new java.util.StringTokenizer(strvalue, ".");
List tempList = new ArrayList();
while (st1.hasMoreTokens()) {
tempList.add(st1.nextToken());
}
if ( (tempList != null) && (tempList.size() > 0)) {
tempArrList.add(tempList);
}
}
return tempArrList;
}
/**
* 根据时间返回8位随机数
*/
public static String getSeed() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(new Date());
}
/**
* 过滤特殊字符,转换为标准html格式
* @param input String 待过滤的字符串
* @return String 过滤好的字符串
*/
public static String escapeHTMLTags(String input) {
String returnStr = "";
if (input == null || input.length() == 0) {
return returnStr;
}
else {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch == '<') {
buf.append("<");
}
else if (ch == '>') {
buf.append(">");
}
else if (ch == '"') {
buf.append(""");
}
else if (ch == '&') {
buf.append("&");
}
else if (ch == ' ') {
buf.append(" ");
}
else {
buf.append(ch);
}
}
returnStr = buf.toString();
String tempStr = "";
StringTokenizer strToken = new StringTokenizer(buf.toString(), "\n");
while (strToken.hasMoreTokens()) {
if (tempStr.length() > 0)
tempStr = tempStr + "<br>" + strToken.nextToken();
else
tempStr = strToken.nextToken();
}
returnStr = tempStr;
}
return returnStr;
}
/**
* 处理null的字符串,返回""
* @param str String 待处理的字符串
* @return String 处理好的字符串
*/
public static String isNull(String str) {
return str == null ? "" : str;
}
/**
* 取显示器屏幕的宽度
* @return int 宽度,单位是象素
*/
public static int getScreenWidth() {
int width = 800;
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension ds = tk.getScreenSize();
width = ds.width;
return width;
}
/**
* 获得n位随机数
* @param length int 要生成随机数的位数
* @return String 随机数
*/
public synchronized final static String generateId(int length) {
Random random = new Random();
StringBuffer result = new StringBuffer();
for (int i = 0; i < length; i++) {
result = result.append(random.nextInt(10));
}
return result.toString();
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
* 定精度,以后的数字四舍五入。
* @param v1 被除数
* @param v2 除数
* @param scale 表示表示需要精确到小数点以后几位。
* @return 两个参数的商
*/
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 获得随机数8位,从a-z,A-Z,0-9中取
* @return String 随机数
*/
public static String getRandomStr() {
RandomStrg random = new RandomStrg();
String tReturn = "";
try {
random.setCharset("a-zA-Z0-9");
random.generateRandomObject();
tReturn = random.getRandom();
}
catch (Exception e) {
Debug.println("get random fail.");
}
return tReturn;
}
/**
* 功能:辅助完成待查数据的去舍
* @param dTransDT String 实际余额日期
* @param dGetBalanceDT String 取余额时间
* @return boolean true:该数据是正确数据;false:该数据还待确定
*/
public static boolean checkDate(String dTransDT, String dGetBalanceDT) {
if (dTransDT == null || dGetBalanceDT == null)
return false;
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dTransDTS = "";
String dGetBalanceDTS = "";
Date dTransDTD = null;
Date dGetBalanceDTD = null;
try {
//取实际余额日期的年月日
dTransDTS = dTransDT.trim().substring(0, 10);
// Debug.println("checkDate() -- 1." + dTransDTS);
//实际余额日期加一天
dTransDTD = df1.parse(dTransDTS);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dTransDTD);
calendar.add(5, 1);
dTransDTD = calendar.getTime();
// Debug.println("checkDate() -- 2." + dTransDTD);
//取取余额时间的年月日
dGetBalanceDTD = df1.parse(dGetBalanceDT);
// System.out.println("checkDate() -- 3." + dGetBalanceDTD);
//取余额时间 - 实际余额日期加一天
long diff = dGetBalanceDTD.getTime() - dTransDTD.getTime();
if (diff < 0)
return false;
//取余额时间
dGetBalanceDTD = df2.parse(dGetBalanceDT);
// Debug.println("checkDate() -- 4." + dGetBalanceDTD);
//应该的取余额时间
dTransDTS = df2.format(dTransDTD).trim().substring(0, 10);
dTransDTD = df2.parse(dTransDTS + " 12:00:00");
// Debug.println("checkDate() -- 5." + dTransDTD);
//取余额时间 - 应该的取余额时间
diff = dGetBalanceDTD.getTime() - dTransDTD.getTime();
// Debug.println("checkDate() -- 6." + diff);
if (diff < 0)
return false;
}
catch (ParseException pe) {
return false;
}
return true;
}
/**
* 返回浮点数的绝对值
* @param s double 待处理数
* @return double 待处理数的绝对值
*/
public static double abs(double s) {
if (s < 0) s = 0 - s;
return s;
}
// public static void main(String[] args) {
// Tool tool = new Tool();
// tool.delPic("D:/jboss-3.2.3/server/default/deploy/TFMS.war/images/count/",
// "Mnager");
// }
public static String dateToString(Date inputDate, String fmt) {
if (inputDate == null)
return "";
SimpleDateFormat theDateFormat = new SimpleDateFormat(fmt);
return theDateFormat.format(inputDate);
}
public static Date stringToDate(String input, String format) throws DataFormatException {
Date result = null;
SimpleDateFormat theDateFormat = new SimpleDateFormat(format);
if (input==null) {
throw new DataFormatException("Null Value");
}
try {
ParsePosition pos = new ParsePosition(0);
result = theDateFormat.parse(input, pos);
} catch (Exception e) {
throw new DataFormatException("Wrong Format");
}
return result;
}
public static String stringsToSQL(String[] strings){
if (strings==null||strings.length==0){
return null;
}
StringBuffer sb=new StringBuffer();
for (int i=0;i<strings.length;i++){
if (i!=0)
sb.append(",");
sb.append("'").append(strings[i]).append("'");
}
return sb.toString();
}
private static boolean isNullObj(Object obj){
if (obj==null)
return true;
if (obj instanceof String){
if ("".equals(obj))
return true;
if ("null".equals(obj))
return true;
if ("--全部--".equals(obj))
return true;
if ("0".equals(obj))
return true;
}
return false;
}
/*返回当前日期 前指定月份的日期 例如month_i=3 当前日期2005-9-10 返回 2005-06-01
* */
public static String getBeforeMonthStr(int month_i){
Date date = new Date();
String year = (new SimpleDateFormat("yyyy")).format(date);
String month = (new SimpleDateFormat("M")).format(date);
if(Integer.parseInt(month)-month_i>0){
try{
date = (new SimpleDateFormat("yyyy-MM-dd")).parse(
year+"-"+String.valueOf(Integer.parseInt(month)-month_i)+"-01");
}catch(Exception ex){
date = new Date();
}
}else{
year=Integer.parseInt(year)-1+"";
try{
date = (new SimpleDateFormat("yyyy-MM-dd")).parse(
year+"-"+String.valueOf(12+Integer.parseInt(month)-month_i)+"-01");
}catch(Exception ex){
date = new Date();
}
}
return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
}
public static Map getStockIOTypeName(){
Map namelist=new HashMap();
namelist.put("A_01","订购入库");
namelist.put("B_01","维修转送");
namelist.put("B_02","维修接收");
namelist.put("B_03","维修送厂");
namelist.put("B_04","维修厂家返回");
namelist.put("B_05","维修返回用户");
namelist.put("B_06","维修返回入库");
namelist.put("C_01","借用出库");
namelist.put("C_02","借用入库");
namelist.put("C_03","借用返回");
namelist.put("C_04","借用返回入库");
namelist.put("C_05","厂家借用入库");
namelist.put("C_06","返回厂家出库");
namelist.put("D_01","调拨出库");
namelist.put("D_02","调拨入库");
namelist.put("D_03","工程移交入库");
namelist.put("E_01","报废出库");
namelist.put("F_01","盘点盘亏出库");
namelist.put("F_02","盘点盘盈入库");
namelist.put("G_00","扩容领用出库");
namelist.put("G_01","故障领用出库");
namelist.put("G_02","领用返回入库");
namelist.put("G_03","坏板修复领用出库");
namelist.put("H_00","初始入库");
namelist.put("H_01","坏板入库");
namelist.put("H_02","其他入库");
namelist.put("H_03","其它出库");
namelist.put("H_04","其它临时出库");
namelist.put("H_05","其它临时出库返回");
namelist.put("H_06","其它临时入库");
namelist.put("H_07","其它临时入库返回");
return namelist;
}
/**
* 格式化数字
* @param num 数字
* @param bitcount 小数位数
* @return
*/
public static String ForMatNum(float num,int bitcount){
String partern="0.";
for (int i=0;i<bitcount;i++)
partern=partern+"0";
java.text.DecimalFormat df=new java.text.DecimalFormat(partern);
return df.format(num);
}
public static String strISO1ToGB2312(String s)
{
if(s == null)
return "";
char orig[] = s.toCharArray();
byte dest[] = new byte[orig.length];
for(int i = 0; i < orig.length; i++)
dest[i] = (byte)(orig[i] & 0xff);
try
{
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
String s1 = new String(toChar.convertAll(dest));
return s1;
}
catch(Exception e)
{
System.out.println(e);
}
String s2 = s;
return s2;
}
public static String strGB2312ToISO1(String s)
{
if(s == null)
return "";
try
{
CharToByteConverter toByte = CharToByteConverter.getConverter("gb2312");
byte orig[] = toByte.convertAll(s.toCharArray());
char dest[] = new char[orig.length];
for(int i = 0; i < orig.length; i++)
dest[i] = (char)(orig[i] & 0xff);
String s2 = new String(dest);
return s2;
}
catch(Exception e)
{
System.out.println(e);
}
String s1 = s;
return s1;
}
}