• Java生成32位全局唯一id


    工具类实现代码如下:

    import java.io.IOException;
    import java.net.InetAddress;
    import java.security.SecureRandom;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Random;
    import java.util.concurrent.atomic.AtomicLong;
     
    import org.apache.commons.lang.StringUtils;
     
    /** 
     * @ClassName: UniqId 
     * @Description: 生成32位全局唯一id
     */
    public class UniqId {
    	
    	private static UniqId me = new UniqId();
        private String hostAddr;
        private final Random random = new SecureRandom();
        private final UniqTimer timer = new UniqTimer();
        
        private boolean isOutputInfo = false;
        
        private UniqId() {
            try {
                final InetAddress addr = InetAddress.getLocalHost();
     
                hostAddr = addr.getHostAddress();
            }
            catch (final IOException e) {
                System.err.println("[UniqID] Get HostAddr Error"+e);
                hostAddr = String.valueOf(System.currentTimeMillis());
            }
     
            if (null == hostAddr || hostAddr.trim().length() == 0 || "127.0.0.1".equals(hostAddr)) {
                hostAddr = String.valueOf(System.currentTimeMillis());
                
            }
            hostAddr = hostAddr.substring(hostAddr.length()-2).replace(".", "0");
            
            if(isOutputInfo){
            	System.out.println("[UniqID]hostAddr is:" + hostAddr + "----length:"+hostAddr.length());
            }
        }
        
        
        /**
         * 获取UniqID实例
         * 
         * @return UniqId
         */
        public static UniqId getInstance() {
        	me.isOutputInfo = false;
            return me;
        }
        
        /**
         * 获取UniqID实例
         * 
         * @return UniqId
         */
    	public static UniqId getInstanceWithLog() {
        	me.isOutputInfo = true;
            return me;
        }
     
     
        /**
         * 获得不会重复的毫秒数
         * 
         * @return 不会重复的时间
         */
        public String getUniqTime() {
        	String time = timer.getCurrentTime();
        	if(isOutputInfo){
        		System.out.println("[UniqID.getUniqTime]" + time +"----length:"+ time.length());
        	}
            return time;
        }
        
        /**
         * 获得UniqId
         * 
         * @return uniqTime-randomNum-hostAddr-threadId
         */
        public String getUniqID() {
            final StringBuffer sb = new StringBuffer();
            final String t = getUniqTime();
            int randomNumber = random.nextInt(8999999) + 1000000;
            sb.append(t);
            sb.append(hostAddr);
            sb.append(getUniqThreadCode());
            sb.append(randomNumber);
            if (isOutputInfo) {
                System.out.println("[UniqID.randomNumber]" + randomNumber+"----length:"+String.valueOf(randomNumber).length());
                System.out.println("[UniqID.getUniqID]" + sb.toString()+"----length:"+String.valueOf(sb).length());
            }
            return sb.toString();
        }
        
        public String getUniqThreadCode(){
        	String threadCode = StringUtils.left(String.valueOf(Thread.currentThread().hashCode()),9);
        	if (isOutputInfo) {
                System.out.println("[UniqID.getUniqThreadCode]" +threadCode+"----length:"+threadCode.length());
            }
        	return StringUtils.leftPad(threadCode, 9, "0");
        }
        
        /**
         * 实现不重复的时间
         */
        private class UniqTimer {
            private final AtomicLong lastTime = new AtomicLong(System.currentTimeMillis());
     
            public String getCurrentTime() {
            	if(!timestamp2Date(this.lastTime.incrementAndGet()).equals(timestamp2Date(System.currentTimeMillis()))){
            		lastTime.set(System.currentTimeMillis()+random.nextInt(10000));
            	}
            	return timestamp2Datetimes(this.lastTime.incrementAndGet());
            }
        }
        
        /**
    	 * 规范化日期,规范成yyyy-MM-dd
    	 * @param timestamp
    	 * @return
    	 */
    	public static String timestamp2Date(long timestamp){
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    		return dateFormat.format(new Date(timestamp * 1000));
    	}
        
    	private static String timestamp2Datetimes(long timestamp){
    		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    		return dateFormat.format(new Date(timestamp));
    	}
        
    }
    
    
    测试代码如下:
    
    package com.test;
     
    public class test {
    	public static void main(String[] args) {
    		//打印生成过程
    		UniqId.getInstanceWithLog().getUniqID();
    		
    		//建议如下使用
    //		String uid = UniqId.getInstance().getUniqID();
    //		System.out.println(uid);
    //		System.out.println(uid.length());
     
    	}
    }
    

      

    上述代码依赖org.apache.commons.lang.StringUtils,介绍如下两种依赖的方式:

    方式一:Maven项目的依赖

    如果你的项目是maven项目,那么请确保项目的pom.xml有如下依赖:

    commons-lang 或者commons-lang3

  • 相关阅读:
    有用网站
    html5页面布局总结
    video和audio支持格式
    关于浏览器缓冲
    java常见面试题汇总
    jvm常用相关参数
    规律字符串拼接
    线程基础知识
    Kafka学习
    Redis学习
  • 原文地址:https://www.cnblogs.com/achengmu/p/13647799.html
Copyright © 2020-2023  润新知