地质所 沉降监测网建设项目
zmk
2024-05-15 9e3afc6d0fa514f986d3fea40fa23124e6fb5070
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
package com.javaweb.common.utils;
 
import java.text.*;
import java.util.Calendar;
 
public class TimeUuidUtil {
    /**
     * The FieldPosition.
     */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);
 
    /**
     * This Format for format the data to special format.
     */
    private final static Format dateFormat = new SimpleDateFormat("YYMMddHHmmss");
    private final static Format dateFormatS = new SimpleDateFormat("YYMMddHHmmssSSS");
    /**
     * This Format for format the number to special format.
     */
    private final static NumberFormat numberFormat = new DecimalFormat("0000");
 
    /**
     * This int is the sequence number ,the default value is 0.
     */
    private static int seq = 0;
 
    private static final Object lock = new Object();
    private static final Object lock2 = new Object();
 
    /**
     * 时间格式生成16位序列ID (注:1秒内最多调用一万次,否则会出现重复)
     * 性能十万次耗时约320ms
     *
     * @return Long
     */
    public static Long get16UUID() {
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        synchronized(lock){
            return getFormatNum(dateFormat,sb,rightNow);
        }
    }
 
    /**
     * 时间格式生成19位序列ID (注:1毫秒内最多调用一万次,否则会出现重复)
     * 注:到2092年时该数字会超出long的范围,会抛异常
     * @return Long
     */
    public static Long get19UUID(){
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        synchronized(lock2){
            return getFormatNum(dateFormatS,sb,rightNow);
        }
    }
 
    private static Long getFormatNum(Format dateFormat,StringBuffer sb,Calendar rightNow){
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        numberFormat.format(seq, sb, HELPER_POSITION);
        if (seq == 9999) {
            seq = 0;
        } else {
            seq++;
        }
        return Long.parseLong(sb.toString());
    }
 
}