java 雪花算法改造(带日期格式)毫秒级

本文介绍了一个使用Java实现的SnowFlake算法,用于生成全局唯一的ID,包括序列生成、时间戳处理和序列溢出处理。通过实例展示了ID的生成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Date;

public class MySnowFlake {
    private long lastTimestamp = -1L;
    private int sequence;
    private final int sequenceMask=999;
    public MySnowFlake(Integer sequence){
        if (sequence<0||sequence>sequenceMask){
            throw new IllegalArgumentException(
                    "sequence can't be greater than 999 or lesser than 0"
            );
        }
        this.sequence=sequence;

    }
    public synchronized String nextId() {
        String result="";
        long timestamp = timeGen();
        if (timestamp < lastTimestamp) {
            System.err.printf(
                    "clock is moving backwards. Rejecting requests until %d.", lastTimestamp);
            throw new RuntimeException(
                    String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
                            lastTimestamp - timestamp));
        }
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) % sequenceMask;

            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            //从0开始
            sequence = 0;
        }
        lastTimestamp=timestamp;
        Date date1 =new Date(lastTimestamp);
        SimpleDateFormat sd =new SimpleDateFormat("yyyyMMdd-HH-mm-ss-SSS");
        String format = sd.format(date1);
        result=format+"-"+sequence;
        return result;
    }

    private long timeGen(){
        return System.currentTimeMillis();
    }
    /**
     * 等待到下一毫秒
     * @param lastTimestamp
     * @return
     */
    private long tilNextMillis(long lastTimestamp) {

        long timestamp = timeGen();

        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }



    public static void main(String[] args) {
        MySnowFlake mySnowFlake =new MySnowFlake(1);
        for (int i=0;i<40;i++){
            String s = mySnowFlake.nextId();
            System.out.println(s);
        }
    }
}



20210706-20-14-47-564-0
20210706-20-14-47-605-0
20210706-20-14-47-605-1
20210706-20-14-47-605-2
20210706-20-14-47-606-0
20210706-20-14-47-606-1
20210706-20-14-47-606-2
20210706-20-14-47-606-3
20210706-20-14-47-606-4
20210706-20-14-47-607-0
20210706-20-14-47-607-1
20210706-20-14-47-607-2
20210706-20-14-47-607-3
20210706-20-14-47-607-4
20210706-20-14-47-607-5
20210706-20-14-47-608-0
20210706-20-14-47-608-1
20210706-20-14-47-608-2
20210706-20-14-47-608-3
20210706-20-14-47-609-0
20210706-20-14-47-609-1
20210706-20-14-47-609-2
20210706-20-14-47-609-3
20210706-20-14-47-609-4
20210706-20-14-47-610-0
20210706-20-14-47-610-1
20210706-20-14-47-610-2
20210706-20-14-47-610-3
20210706-20-14-47-611-0
20210706-20-14-47-611-1
20210706-20-14-47-611-2
20210706-20-14-47-611-3
20210706-20-14-47-611-4
20210706-20-14-47-611-5
20210706-20-14-47-611-6
20210706-20-14-47-611-7
20210706-20-14-47-612-0
20210706-20-14-47-612-1
20210706-20-14-47-612-2
20210706-20-14-47-612-3

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值