//获取16位String类型id
public static String getStringId(){
int randomCode = 0;
int randomSize = 1;
int len = 3;//返回的长度,例:len=3返回100-999之间的数
for(int i = 0; i<len; i++){
randomSize = randomSize*10;
}
do {
randomCode = (int)(Math.random()*randomSize);
} while (randomCode<(randomSize/10));
//获取13位的毫秒级时间戳
long time = new Date().getTime();
String id_str = time + "" + randomCode;
return id_str;
}
// 返回String类型的时间
public static String getDateTime_String() {
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
// 返回String类型的时间
public static String getDateTime_String_nomark() {
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return df.format(date);
}
// 返回String类型的时间 精确到毫米
public static String getDateTimeMi_String() {
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
return df.format(date);
}
// 返回Date类型的时间
public static Date getDateTime_Date() {
String dtd = getDateTime_String();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return df.parse(dtd);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
//传入长度 获取随机码
public static String getRandomCode(int len){
int randomCode = 0;
int randomSize = 1;
for(int i = 0; i<len; i++){
randomSize = randomSize*10;
}
do {
randomCode = (int)(Math.random()*randomSize);
} while (randomCode<(randomSize/10));
String randomCode_String = randomCode+"";
return randomCode_String;
}