WEB开发网
开发学院软件开发Java java根据时间生成唯一ID 阅读

java根据时间生成唯一ID

 2012-05-24 15:27:14 来源:WEB开发网   
核心提示:php;">import java.text.DecimalFormat;import java.text.FieldPosition;import java.text.Format;import java.text.NumberFormat;import java.text.SimpleDateFormat
php;">
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(ReadFileUtil.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();
    }
}

Tags:java 根据 时间

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接