📄 stringutil.java
字号:
package com.bookstore.util;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.StringTokenizer;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
*
* @author zhanghong
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* MSN:
*/
public class StringUtil
{
/**
* @param srcDate
* @return
* @deprecated 只用于日期为dd/MM/yyyy的日期转换
*/
public static String ConvertDate(String srcDate)
{
String[] date = GetTokens(srcDate, "/");
return new String(date[2] + "-" + date[1] + "-" + date[0]);
}
public static String Base64Encoder(String str)
{
if (str != null && !str.equals(""))
{
str = new BASE64Encoder().encode(str.getBytes());
return str;
}
else
return str;
}
public static String Base64Decoder(String str)
{
if (str == null || str.equals(""))
return str;
try
{
return new String(new BASE64Decoder().decodeBuffer(str));
}
catch (Exception e)
{
return null;
}
}
public static boolean isSameDate(String strSrcDate, String strDesDate)
{
String[] srcDate = GetTokens(strSrcDate, "-");
String[] desDate = GetTokens(strDesDate, "-");
if (srcDate[0].equals(desDate[0]) && srcDate[1].equals(desDate[1]) && srcDate[2].equals(desDate[2]))
return true;
return false;
}
public static String[] GetTokens(String str, String token)
{
StringTokenizer st = new StringTokenizer(str, token);
String subStr[] = new String[st.countTokens()];
for (int i = 0; i < subStr.length; i++)
subStr[i] = st.nextToken();
return subStr;
}
public static String SetTokens(String[] str, String token)
{
String subStr = "";
for (int i = 0; i < str.length; i++)
{
subStr += str[i];
if (i != str.length - 1)
subStr += token;
}
return subStr;
}
public static String getDateString(long lDateTime, String fmt)
{
SimpleDateFormat ft = new SimpleDateFormat(fmt);
if (fmt == null || fmt.equals(""))
fmt = new String("yyyy-MM-dd hh:mm:ss");
Date date;
if (lDateTime == 0)
date = new Date(System.currentTimeMillis());
else
date = new Date(lDateTime);
String dateStr = ft.format(date);
return dateStr;
}
public static String getDateString(Date DateTime, String fmt)
{
if (fmt == null || fmt.equals(""))
fmt = new String("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat ft = new SimpleDateFormat(fmt);
String dateStr = ft.format(DateTime);
return dateStr;
}
public static String genDateString()
{
return genDateString("yyyy-MM-dd");
}
public static String genDateTimeString()
{
return genDateString("yyyy-MM-dd HH:mm:ss");
}
public static String genDateString(String fmt)
{
SimpleDateFormat ft = new SimpleDateFormat(fmt);
Date now = new Date(System.currentTimeMillis());
String dateStr = ft.format(now);
return dateStr;
}
public static String ToEncoding(String l_S_Source, String encoding) throws UnsupportedEncodingException
{
String l_S_Desc = "";
if (l_S_Source != null && !l_S_Source.trim().equals(""))
{
byte l_b_Proc[] = l_S_Source.getBytes(encoding);
l_S_Desc = new String(l_b_Proc);
}
return l_S_Desc;
}
public static String RandomNumber()
{
Random rdm = new Random(System.currentTimeMillis());
return Long.toString(Math.abs(rdm.nextLong()));
}
public static String replaceAll(String s, String oldToken, String newToken)
{
String result = s;
StringBuffer sb = new StringBuffer(result);
int pos, start = 0;
while ((pos = result.indexOf(oldToken, start)) != -1)
{
sb.replace(pos, pos + oldToken.length(), newToken);
start = pos + newToken.length();
result = sb.toString();
}
return result;
}
public static String toISO8859_1(String src)
{
String s = "";
try
{
s = new String(src.getBytes(), "ISO8859_1");
}
catch (Exception e)
{
}
return s;
}
public static String toGB2312(String src)
{
String s = "";
try
{
s = new String(src.getBytes(), "gb2312");
}
catch (Exception e)
{
}
return s;
}
public static String toGBISO(String src)
{
String s = "";
try
{
s = new String(src.getBytes("gb2312"), "iso8859_1");
}
catch (Exception e)
{
}
return s;
}
public static String toISOGB(String src)
{
String s = "";
try
{
s = new String(src.getBytes("iso8859_1"), "gb2312");
}
catch (Exception e)
{
}
return s;
}
public static String toUTF8(String src)
{
String s = "";
try
{
s = new String(src.getBytes(), "UTF-8");
}
catch (Exception e)
{
}
return s;
}
public static String toGBUTF8(String src)
{
String s = "";
try
{
s = new String(src.getBytes("gb2312"), "UTF-8");
}
catch (Exception e)
{
}
return s;
}
public static String toUTF8GB(String src)
{
String s = "";
try
{
s = new String(src.getBytes("UTF-8"), "gb2312");
}
catch (Exception e)
{
}
return s;
}
/*public static String getRandCode(int i)
{
String ret = "";
Random rand = new Random();
ret = String.valueOf(rand.nextInt(i));
return ret;
}
*/
public static String getRandCode(int i)
{
String ret = "";
for(int k=0;k<i;k++)
{
Random rand = new Random();
ret += String.valueOf(rand.nextInt(9));
}
return ret;
}
public String firstToUpperCase(String str)
{
String s1 = str.substring(0, 1);
String s2 = str.substring(1);
return s1.toUpperCase() + s2;
}
//MD5加密
public static String md5Encrypt(String input)
{
return org.apache.commons.codec.digest.DigestUtils.md5Hex(input);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -