常用基础函数
类别: JSP教程
package com.cdc;
import java.util.*;
import java.io.*;
/**
* 常用基础函数(包括合并数组,拆分字串到数组,生成随机数,替换字符串中的子字符串,获取当前时间),将HASHTABLE内容转入数组
*/
public class Util {
/**
* 拼合数组。
* @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中。
* @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;
}
/**
* 替换字符串中的子字符串
* @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
* @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[]
* @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;
}
/**
* 拆分字串到数组,分割符可使用多个字符或者中文
* @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)参数为时区
* @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内容转入数组
* @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;
}
}
import java.util.*;
import java.io.*;
/**
* 常用基础函数(包括合并数组,拆分字串到数组,生成随机数,替换字符串中的子字符串,获取当前时间),将HASHTABLE内容转入数组
*/
public class Util {
/**
* 拼合数组。
* @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中。
* @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;
}
/**
* 替换字符串中的子字符串
* @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
* @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[]
* @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;
}
/**
* 拆分字串到数组,分割符可使用多个字符或者中文
* @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)参数为时区
* @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内容转入数组
* @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;
}
}
-= 资 源 教 程 =-
文 章 搜 索