57 lines
1.3 KiB
Plaintext
57 lines
1.3 KiB
Plaintext
package com.ifish.util;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
import java.util.Random;
|
|
|
|
public class IfishUtil {
|
|
|
|
private static final String formatDate1 ="yyyy-MM-dd";
|
|
private static final String formatDate2 ="yyyy-MM-dd HH:mm:ss";
|
|
|
|
/**
|
|
* 格式化日期yyyy-MM-dd HH:mm:ss
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static String format2(Date date){
|
|
SimpleDateFormat format = new SimpleDateFormat(formatDate2);
|
|
return format.format(date);
|
|
}
|
|
|
|
/**
|
|
* 格式化日期yyyy-MM-dd
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static String format1(Date date){
|
|
SimpleDateFormat format = new SimpleDateFormat(formatDate1);
|
|
return format.format(date);
|
|
}
|
|
|
|
/**
|
|
* 随机生成字母与数字组合
|
|
* @param length
|
|
* @return
|
|
*/
|
|
public static String getCharAndNumr(int length) {
|
|
String val = "";
|
|
Random random = new Random();
|
|
for (int i = 0; i < length; i++) {
|
|
// 输出字母还是数字
|
|
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
|
|
// 字符串
|
|
if ("char".equalsIgnoreCase(charOrNum)) {
|
|
// 取得大写字母还是小写字母
|
|
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
|
|
val += (char) (choice + random.nextInt(26));
|
|
// 数字
|
|
} else if ("num".equalsIgnoreCase(charOrNum)) {
|
|
val += String.valueOf(random.nextInt(10));
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
}
|