---
title: "java 生成唯一ID "
author: "shaq34"
date: "2009-08-01T11:21:37Z"
tags: []
slug: "java-id"
draft: false
lastmod: "2009-08-21T10:21:26Z"
id: 437478
url: "https://shaq34.iteye.com/blog/437478"
---

针对JDK1.4之前没有UUID


```java

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;
	}
}

```