Java时间、随机数工具类

//获取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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值