现在的位置: 首页 > 综合 > 正文

java 生成唯一ID

2012年08月26日 ⁄ 综合 ⁄ 共 755字 ⁄ 字号 评论关闭
针对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(032).toUpperCase();   
  
        
return uuid;   
    }   
}  

 

抱歉!评论已关闭.