All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hdy.common.util.IdWorker Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
package com.github.hdy.common.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。
 * 优化开源项目 https://gitee.com/yu120/sequence
 *
 * @author 贺大爷
 * @date 2019/6/25
 */
public class IdWorker {
    /**
     * 主机和进程的机器码
     */
    private static Sequence WORKER = new Sequence();

    /**
     * 毫秒格式化时间
     */
    public static final DateTimeFormatter MILLISECOND = DateTimeFormatter.ofPattern(Dates.FORMAT_NONE);

    /**
     * 标准UUID(36位)
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 使用ThreadLocalRandom获取UUID获取更优的效果 去掉"-"
     */
    public static String get32UUID() {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        return new UUID(random.nextLong(), random.nextLong()).toString().replace("-", "");
    }

    /**
     * 19位
     */
    public static long getId() {
        return WORKER.nextId();
    }

    public static String getIdStr() {
        return String.valueOf(WORKER.nextId());
    }

    /**
     * 格式化的毫秒时间
     */
    public static String getMillisecond() {
        return LocalDateTime.now().format(MILLISECOND);
    }

    /**
     * 时间 ID = Time + ID
     * 

例如:可用于商品订单 ID

*/ public static String getTimeId() { return getMillisecond() + getId(); } /** * 有参构造器 * * @param workerId 工作机器 ID * @param datacenterId 序列号 */ public static void initSequence(long workerId, long datacenterId) { WORKER = new Sequence(workerId, datacenterId); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy