直接上代码:
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;
}
}
}
}