2007-02-07 17:38:19逸&瑩

幾個常用基礎函數(JSP)

package com.cdc;

import java.util.*;
import java.io.*;

/**
* 常用基礎函數(包括合併陣列,拆分字串到陣列,生成隨機數,替換字串中的子字串,取得當時時間),將HASHTABLE內容轉入陣列
* 建立日期:(2001-7-19 17:22:33)
* @author:劉曉東
*/

public class Util {

/**
* 拼合陣列。
* 建立日期:(2001-8-13 17:08:26)
* @return java.lang.String
* @param param java.lang.String
*/
public String join_array(String[] param,String spilt) {
String rentunstring;
StringBuffer tempstr = new StringBuffer();
int len = param.length;
for (int i = 0 ; i<len-1;i++ )
{
tempstr.append(param[i]);
tempstr.append(spilt);
}
tempstr.append(param[len-1]);
rentunstring = tempstr.toString();


return rentunstring;
}


/**
* 產生在start和end之間的num個隨機整數,傳回值存在Hashtable中。
* 建立日期:(2001-7-30 8:50:23)
* @return java.util.Hashtable
* @param start int 起始點
* @param end int 結束點
* @param num int 生成個數
*/
public Hashtable random(int start, int end, int num)
{
Hashtable randomHash = new Hashtable();
for (int i = 0; i < num; i++)
{
double sru = Math.random() * end;
int tag = Math.round((float) sru);
if (tag < start)
{
i--;
} else
{
randomHash.put(new Integer(i), new Integer(tag));
}
}
return randomHash;
}
/**
* 替換字串中的子字串
* 建立日期:(2001-10-10 13:50:21)
* @return java.lang.String
* @param param java.lang.String 原字串
* @param param1 java.lang.String 需要替換的子字串
* @param param2 java.lang.String 將被替換成的子字串
* @exception java.lang.Exception 異常說明。
*/
public String replaceString(
String param,
String param1,
String param2)
throws java.lang.Exception {
String returnString = "";
try
{
if (param != null
&& param1 != null
&& param2 != null
&& !param.equalsIgnoreCase("")
&& !param1.equalsIgnoreCase("")
&& !param2.equalsIgnoreCase(""))
{

int intLen = param.length(); //原字符串長度

int intLenSrc = param1.length(); //需要替換的子字符串長度

int intPoint = 0; //當前位置

while (intPoint < param.length())
{
if ((intPoint + intLenSrc) <= param.length() && intPoint<param.length())
{
String compareString =
param.substring(intPoint, intPoint + intLenSrc);

if (compareString.equalsIgnoreCase(param1))
{
returnString = returnString + param2;

intPoint = intPoint + param1.length();
}
else
{
returnString = returnString + param.charAt(intPoint);
intPoint++;

}


}
else if ((intPoint + intLenSrc) > param.length() && intPoint<param.length())
{
returnString = returnString + param.charAt(intPoint);
intPoint++;

}
else if ((intPoint + intLenSrc) > param.length())
{
break;
}

}
}

}
catch (java.lang.Exception ex)
{

return ex.toString();

}

return returnString;
}
/**
* 此處插入方法說明
* 拆分字串到陣列,分割符請使用,.等,不能使用中文符號作為分割符號,返回Hashtable
* 建立日期:(2001-7-10 14:50:31)
* @param fieldsru java.lang.String 輸入參數:待拆分字符串
* @param tag java.lang.String 輸入參數:分割符
* 返回Hashtable
* @exception java.lang.Exception 異常說明。
* @exception java.io.IOException 異常說明。
*/
public Hashtable spilt(String fieldsru, String tag)
{
Hashtable returnarray = new Hashtable();
char dot = tag.charAt(0);
String field;
field = fieldsru + dot;
int num = 0;
int field_len = field.length();
for (int i = 0; i < field_len; i++)
{
if (field.charAt(i) == dot)
{
num++;
}
}
int begin = 0;
int end;
for (int j = 0; j < num; j++)
{

end = field.indexOf(dot, begin);
returnarray.put(new Integer(j), field.substring(begin, end));
begin = end + 1;
}

return returnarray;
}


/**
* 拆分字串到陣列,分割符請使用,.等,不能使用中文符號作為分割符號,返回String[]
* 建立日期:(2001-7-10 14:50:31)
* @param fieldsru java.lang.String 輸入參數:待拆分字符串
* @param tag java.lang.String 輸入參數:分割符
* 返回String[]
* @exception java.lang.Exception 異常說明。
* @exception java.io.IOException 異常說明。
*/
public String[] spilt_str(String fieldsru, String tag)
{
//Hashtable returnarray = new Hashtable();
char dot = tag.charAt(0);
String field;
field = fieldsru + dot;
int num = 0;
int field_len = field.length();
for (int i = 0; i < field_len; i++)
{
if (field.charAt(i) == dot)
{
num++;
}
}
String[] returnarray = new String[num];
int begin = 0;
int end;
for (int j = 0; j < num; j++)
{

end = field.indexOf(dot, begin);
returnarray[j] = field.substring(begin, end);
//returnarray.put(new Integer(j), field.substring(begin, end));
begin = end + 1;
}

return returnarray;
}
/**
* 拆分字串到陣列,分割符可使用多個字符或者中文
* 建立日期:(2001-7-10 14:50:31)
* @param fieldsru java.lang.String 輸入參數:待拆分字符串
* @param tag java.lang.String 輸入參數:分割符
* @exception java.lang.Exception 異常說明。
* @exception java.io.IOException 異常說明。
*/
public String[] SplitString(String fieldsru, String tag)
{
try
{
int dot_len = tag.length(); //獲得分割符的長度
int str_len = fieldsru.length(); //字串長度
String tempStr = fieldsru + tag;
int num = 0;
for (int i = 0; i < str_len; i++)
{
int point = i + dot_len;
if (point < str_len)
{
String temp = fieldsru.substring(i, point);
if (temp.equalsIgnoreCase(tag))
{
num++;
}
}
}
num = num + 1;
String[] returnarray = new String[num];
int begin = 0;
int end = fieldsru.indexOf(tag);
for (int i = 0; i < num; i++)
{

end = tempStr.indexOf(tag, begin);
returnarray[i] = tempStr.substring(begin, end);
begin = end + dot_len;
}
return returnarray;
}
catch (java.lang.Exception e)
{
System.out.println(e.getMessage());

}
return null;
}

/**
* 取得當時時間()時間格式 (mm/dd/yyyy hh:mm:ss pm)參數為時區
* 建立日期:(2001-10-22 10:11:48)
* @return java.lang.String
* @exception java.lang.Exception 異常說明。
*/
public String gettime(int time_zone) throws java.lang.Exception {

String nowtime = "";
try
{
if (time_zone > 0)
{
time_zone = time_zone - 1;
}
else if (time_zone < 0)
{
time_zone = time_zone + 1;
}

String[] ids = TimeZone.getAvailableIDs(time_zone * 60 * 60 * 1000);
SimpleTimeZone pdt =
new SimpleTimeZone(time_zone * 60 * 60 * 1000, ids[0]);
pdt.setStartRule(
Calendar.APRIL,
1,
Calendar.SUNDAY,
2 * 60 * 60 * 1000);
pdt.setEndRule(
Calendar.OCTOBER,
-1,
Calendar.SUNDAY,
2 * 60 * 60 * 1000);

Calendar calendar = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar.setTime(trialTime);
String now_year = new Integer(calendar.get(Calendar.YEAR)).toString();
String now_month =
new Integer(calendar.get(Calendar.MONTH) + 1).toString();
if (now_month.length() == 1)
{
now_month = "0" + now_month;
}
String now_date = new Integer(calendar.get(Calendar.DATE)).toString();
if (now_date.length() == 1)
{
now_date = "0" + now_date;
}
int am_pm = calendar.get(Calendar.AM_PM);
String now_hour = new Integer(calendar.get(Calendar.HOUR)).toString();
if (am_pm == 1)
{
int intnow_hour = Integer.parseInt(now_hour) + 12;
now_hour = (new Integer(intnow_hour)).toString();

}
else
{
}
if (now_hour.length() == 1)
{

now_hour = "0" + now_hour;

}

String now_MINUTE =
new Integer(calendar.get(Calendar.MINUTE)).toString();
if (now_MINUTE.length() == 1)
{
now_MINUTE = "0" + now_MINUTE;
}

String now_SECOND =
new Integer(calendar.get(Calendar.SECOND)).toString();
if (now_SECOND.length() == 1)
{
now_SECOND = "0" + now_SECOND;
}

String str_AMPM = "";
//判斷上午還是下午
if (am_pm == 0)
{
str_AMPM = "AM";
}
else if (am_pm == 1)
{
str_AMPM = "PM";
}
nowtime =
now_month
+ "/"
+ now_date
+ "/"
+ now_year
+ " "
+ now_hour
+ ":"
+ now_MINUTE
+ ":"
+ now_SECOND
+ " "
+ str_AMPM;

}
catch (Exception ex)
{
nowtime = ex.getMessage();

}
return nowtime;
}

/**
* 將HASHTABLE內容轉入陣列
* 建立日期:(2001-10-29 8:58:54)
* @return java.lang.String
* @param param java.util.Hashtable
* @exception java.lang.Exception 異常說明。
*/
public String[] hashToArray(Hashtable param)
throws java.lang.Exception {

if (param.size() == 0)
{
return null;
}
String[] return_array = new String[param.size()];
try
{
int param_size = param.size();
Enumeration enumKeys = param.keys(); //將hashtable轉換為枚舉類型

for (int i = 0; enumKeys.hasMoreElements(); i++)
{
Object temp = enumKeys.nextElement();
return_array[i] = (String) param.get(temp);
}
}
catch (Exception ex)
{
return_array[0] = ex.getMessage();
}
return return_array;
}
}