public String getMyDate(int type)
{
String date="";
Calendar cal =
Calendar.getInstance();
int
pm_am=cal.get(Calendar.AM_PM);
int hour=cal.get(Calendar.HOUR);
if(pm_am==Calendar.PM) hour+=12;
//字符分割方法
public static final String[] split(String str,
String delims)
{
StringTokenizer st = new
StringTokenizer(str, delims);
ArrayList list = new
ArrayList();
for(;
st.hasMoreTokens(); list.add(st.nextToken()));
return
(String[])list.toArray(new String[list.size()]);
}
10、JSP与Servlet处理字符乱码方式:
// 用于读数据库时由iso8859-1变为GBK
public static String GBKConverter(String s_string){
try{
String des = new
String(s_string.getBytes("iso8859-1"),"gb2312");
return des;
}
catch(Exception ex){
String des="";
return des;
}
}
// 用于处理页内生成的中文数据在写入数据库时的处理,由GBK变为iso8859-1
public static String ISOConverter(String s_string){
try{
String des = new
String(s_string.getBytes("gb2312"),"iso8859-1");
return des;
}
catch(Exception ex){
String des = "";
return des;
}
}
//
随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 220));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = createRandStr();
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(40 + random.nextInt(110), 40
+ random
.nextInt(110), 40 +
random.nextInt(110)));
//
调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 10 * i + 3, 18);
}
public String createRandStr()
{
//Random random = new Random();
String str="";
String[]
arr={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
//for(int i=0;i<4;i++)
//{
int rand=random.nextInt(61);
str=arr[rand];
//}
return str;
}
Color getRandColor(int fc, int bc) {
// 给定范围获得随机颜色
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}