ITeye博客

java 生成唯一ID

shaq34 · 2009-08-01 11:21 · Markdown

针对JDK1.4之前没有UUID


public class UUID {
	protected static int count = 0;

	public static synchronized String getUUID() {
		count++;
		long time = System.currentTimeMillis();

		String timePattern = Long.toHexString(time);
		int leftBit = 14 - timePattern.length();
		if (leftBit > 0) {
			timePattern = "0000000000".substring(0, leftBit) + timePattern;
		}

		String uuid = timePattern
				+ Long.toHexString(Double.doubleToLongBits(Math.random()))
				+ Long.toHexString(Double.doubleToLongBits(Math.random()))
				+ "000000000000000000";

		uuid = uuid.substring(0, 32).toUpperCase();

		return uuid;
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。