/**
* 生成随机数字
*
* @param length
* @return
*/
public static final String randomNumber(int length) {
char[] numbersAndLetters = null;
java.util.Random randGen = null;
if (length < 1) {
return null;
}
// Init of pseudo random number generator.
if (randGen == null) {
if (randGen == null) {
randGen = new java.util.Random();
// Also initialize the numbersAndLetters array
numbersAndLetters = ("0123456789").toCharArray();
}
}
// Create a char buffer to put random letters and numbers in.
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[randGen.nextInt(9)];
}
return new String(randBuffer);
}
生成随机字符串
/**
* 生成随机字符串
*
* @param length
* @return
*/
public static final String randomString(int length) {
char[] numbersAndLetters = null;
java.util.Random randGen = null;
if (length < 1) {
return null;
}
// Init of pseudo random number generator.
if (randGen == null) {
if (randGen == null) {
randGen = new java.util.Random();
// Also initialize the numbersAndLetters array
numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
}
}
// Create a char buffer to put random letters and numbers in.
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
}
return new String(randBuffer);
}
字符串分割
/**
* 字符串分割
*
* @param oldString
* @param delim
* @return
*/
public static String[] split(String oldString, String delim) {
if (oldString == null)
return null;
String[] newArray = null;
java.util.StringTokenizer st = new java.util.StringTokenizer(oldString,
delim);
newArray = new String[st.countTokens()];
int count = 0;
while (st.hasMoreTokens()) {
newArray[count] = st.nextToken().trim();
count++;
}
return newArray;
}
格式化日期
/**
* 格式化日期
*
* @param d
* @return
*/
public static String formatDate(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(d);
}
/**
* 格式化日期
*
* @param d
* @return
*/
public static String formatDatess(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return sdf.format(d);
}
/**
* 格式化日期
*
* @param d
* @return
*/
public static String formatDates(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(d);
}
/**
* 格式化日期
*
* @param d
* @return
*/
public static String formatDateMM(Date d) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm");
return sdf.format(d);
}
MD5加密
/**
* MD5加密
*
* @param orgString
* @return
* @throws java.security.NoSuchAlgorithmException
* @throws java.io.UnsupportedEncodingException
*/
public static String md5Encrypt(String orgString) {
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
md.update(orgString.getBytes());
byte[] b = md.digest();
return byte2hex(b);
} catch (java.security.NoSuchAlgorithmException ne) {
throw new IllegalStateException(
"System doesn't support your Algorithm.");
}
}
public static String md5Encrypt(String orgString, String charSet) {
try {
java.security.MessageDigest md = java.security.MessageDigest
.getInstance("MD5");
md.update(orgString.getBytes(charSet));
byte[] b = md.digest();
return byte2hex(b);
} catch (java.security.NoSuchAlgorithmException ne) {
throw new IllegalStateException(
"System doesn't support your Algorithm.");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
throw new IllegalStateException("System doesn't support chaset");
}
}
将字节数组转换成16进制字符串
/**
* 将字节数组转换成16进制字符串
*
* @param b
* @return
*/
private static String byte2hex(byte[] b) // 二行制转字符串
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
金钱相关处理
/**
* 从数据库读出钱时,调用这个函数(数据库保存的金额为分,前台显示为元)
*/
public static String formatMoneyFromData(String price) {
if (price == null)
return price;
if (price.indexOf(".") == 0)
return "0";
if (price.indexOf(".") > 0)
price = price.substring(0, price.indexOf("."));
int money = Integer.parseInt(price);
int mod = money % 100;
int imod = money / 100;
String temp = "";
// if ( mod == 0 )
// temp = new String ("00");
// else
if ((mod < 10) && (mod > -10))
temp = ".0" + mod;
else
temp = "." + mod;
return "" + imod + temp;
}
/**
* 将分格式化为元,去掉小数点后的0
*
* @param price
* @return
*/
public static String formatMoney(String price) {
Long money = Long.valueOf(price) / 100;
money.shortValue();
return money.toString();
}
获取两个日期之间的天数
/**
* 获取两个日期之间的天数
*
* @param startDate
* @param endDate
* @return
* @throws ParseException
*/
public static long getCompareDate(String startDate, String endDate)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = formatter.parse(startDate);
Date date2 = formatter.parse(endDate);
long l = date2.getTime() - date1.getTime();
long d = l / (24 * 60 * 60 * 1000);
return d;
}
public static int longToInt(long value) {
return Integer.valueOf(Long.valueOf(value).toString());
}
半角转全角
/**
* 半角转全角
*
* @param input
* String.
* @return 全角字符串.
*/
public static String toSBC(String input) {
if (input == null)
return null;
try {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ') {
c[i] = 'u3000';
} else if (c[i] < '177') {
c[i] = (char) (c[i] + 65248);
}
}
return new String(c);
} catch (Exception e) {
e.printStackTrace();
}
return input;
}
// 取数字字符串 用 splitStr 分割
private String getNumberString() {
String splitStr = " "; // 分割符
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 10; i++) {
buf.append(String.valueOf(i));
buf.append(splitStr);
}
return buf.toString();
}
取大写字母字符串 用 splitStr 分割
// 取大写字母字符串 用 splitStr 分割
private String getUppercase() {
String splitStr = " "; // 分割符
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 26; i++) {
buf.append(String.valueOf((char) ('A' + i)));
buf.append(splitStr);
}
return buf.toString();
}
取小写字母字符串 用 splitStr 分割
// 取小写字母字符串 用 splitStr 分割
private String getLowercase() {
String splitStr = " "; // 分割符
StringBuffer buf = new StringBuffer();
for (int i = 0; i < 26; i++) {
buf.append(String.valueOf((char) ('a' + i)));
buf.append(splitStr);
}
return buf.toString();
}
取特殊字符串 用 splitStr 分割
// 取特殊字符串 用 splitStr 分割
private String getSpecialString() {
String splitStr = " "; // 分割符
String str = "~@#$%^&*()_+|\=-`";
StringBuffer buf = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
buf.append(str.substring(i, i + 1));
buf.append(splitStr);
}
return buf.toString();
}
格式化数字保留两位小数
/**
* 格式化数字保留两位小数
* 如果是144.0或114.00 则返回114 ,如果带小数点则四舍五入到保留2位小数
* @param v
* @return
*/
public static String formatNum(Object val){
String result = "" ;
if(val != "null" && val != null && val != ""){
String v = val.toString();
if(v.indexOf(".") > -1){
if("0".equals(v.split("\.")[1]) || "00".equals(v.split("\.")[1])){
result = v.split("\.")[0];
}else{
Double d = Double.valueOf(v);
result = String.format("%.2f",d);
}
}else{
result = v ;
}
}
return result;
}
获取多少位随机数
/**
* 获取多少位随机数
* @param num
* @return
*/
public static String getNumStringRandom(int num){
StringBuilder str = new StringBuilder();
Random random = new Random();
//随机生成数字,并添加到字符串
for(int i = 0;i<num;i++){
str.append(random.nextInt(10));
}
return str.toString();
}
获取区间内的随机数
/**
* 获取区间内的随机数
* @param min
* @param max
* @return
*/
public static int getRandomBetween(int min, int max){
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
return s;
}
}
随机生成字符串数组中的字符串
Random r =newRandom();
String gsName[]={"G15","G18荣乌高速","264省道","302省道","S19龙青高速"};
String gs = gsName[r.nextInt(4)];sout('gs')