1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.dy.pipIrrWebFile.fm;
 
import java.util.Calendar;
 
public class FileNameIdUtil {
 
    private static int add = 0 ;
    private static int chengShu = 1000 ;
    private static int maxAdd = 999 ;
    private static long last = 0 ;
    
    //后缀
    private static String suffix = "0" ;
 
    static {
        last = current() ;
    }
    
    /**
     * 为自实现程序提供的ID生成器
     * 15长度ID,年度取两位,如果是17位长度ID,年度取四位,那17位数字超出了javascript的表数范围 
     */
    public  String generate(){
        return doGenerate() ;    
    }
    
    /**
     * 设置后缀,不同子系统设置不同的后缀
     * @param suffix
     */
    public static void setSuffix(String suffix)throws Exception{
        if(suffix == null || suffix.trim().equals("")){
            throw new Exception("后缀不能为空") ;
        }
        FileNameIdUtil.suffix = suffix.trim() ;
    }
    
    /**
     * 执行生成
     * @return
     */
    private synchronized String doGenerate(){
        Long id = null ;
        long now = current() ;
        if(now != last){
            //上次生成ID 与本次生成ID 不在同一秒内
            last = now ;
            add = 0 ;
            id = last * chengShu + add ++;
        }else{
            //上次生成ID 与本次生成ID 在同一秒内
            if(add == maxAdd){
                //附加量已经用尽
                waitNextSecond(last) ;//等到下一秒
                id = last * chengShu + add ++ ;//返回上一秒生成的ID
                add = 0 ;//附加量归零,为下一秒准备
            }else{
                //附加量未用尽
                id = last * chengShu + add ++ ;
            }
        }
        return id + suffix ;    
    }
    /**
     * 等待下一秒到来
     * @param last
     */
    private void waitNextSecond(Long last){
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }finally{
            long now = current() ;
            if(now == last){
                waitNextSecond(last) ;
            }
        }
    }
 
   
    
    /**
     * 格式为 150516010203
     * @return
     */
    private static long current(){
        Calendar cal = Calendar.getInstance();
        long d = (cal.get(Calendar.YEAR) % 100) * 10000000000L 
        + (cal.get(Calendar.MONTH) + 1) * 100000000L 
        + cal.get(Calendar.DAY_OF_MONTH) * 1000000L 
        + cal.get(Calendar.HOUR_OF_DAY) * 10000L 
        + cal.get(Calendar.MINUTE) * 100L 
        + cal.get(Calendar.SECOND)  ;
        
        return d ;
    }
 
    public static void main(String args[]){
        FileNameIdUtil o = new FileNameIdUtil() ;
        int total = 800 ;
        long start = System.currentTimeMillis() ;
        for(int i = 0 ; i < total ; i++){
            System.out.println((String)(o.generate())) ;
        }
        long end = System.currentTimeMillis() ;
        System.out.println("产生" + total + "ID用时" + (end - start) + "毫秒");
    }
    
}