首页 > 代码库 > Java工具类:获取long型唯一ID

Java工具类:获取long型唯一ID

技术分享
import java.text.SimpleDateFormat;
import java.util.Date;

/** 
 * 获取long型唯一ID
 */
public class ID
{

    private static long tmpID = 0;

    private static boolean tmpIDlocked = false;

    public static long getId()
    {
        long ltime = 0;
        while (true)
        {
            if(tmpIDlocked == false)
            {
                tmpIDlocked = true;
                //当前:(年、月、日、时、分、秒、毫秒)*10000
                ltime = Long.valueOf(new SimpleDateFormat("yyMMddhhmmssSSS").format(new Date()).toString()) * 10000;
                if(tmpID < ltime)
                {
                    tmpID = ltime;
                }
                else
                {
                    tmpID = tmpID + 1;
                    ltime = tmpID;
                }
                tmpIDlocked = false;
                return ltime;
            }
        }
    }
}
技术分享

Java工具类:获取long型唯一ID