java 时间 id_java根据时间生成唯一ID

import java.text.DecimalFormat;

import java.text.FieldPosition;

import java.text.Format;

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import org.apache.log4j.Logger;

public class GenerateSequenceUtil {

/** .log */

private static final Logger logger = Logger.getLogger(GenerateSequenceUtil.class);

/** The FieldPosition. */

private static final FieldPosition HELPER_POSITION = new FieldPosition(0);

/** This Format for format the data to special format. */

private final static Format dateFormat = new SimpleDateFormat("MMddHHmmssS");

/** This Format for format the number to special format. */

private final static NumberFormat numberFormat = new DecimalFormat("0000");

/** This int is the sequence number ,the default value is 0. */

private static int seq = 0;

private static final int MAX = 9999;

/**

* 时间格式生成序列

* @return String

*/

public static synchronized String generateSequenceNo() {

Calendar rightNow = Calendar.getInstance();

StringBuffer sb = new StringBuffer();

dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);

numberFormat.format(seq, sb, HELPER_POSITION);

if (seq == MAX) {

seq = 0;

} else {

seq++;

}

logger.info("THE SQUENCE IS :" + sb.toString());

return sb.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值