You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

package com.qs.serve.common.util;
import cn.hutool.core.lang.Snowflake;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
/**
* @author YenHex
* @since 2022/3/3
*/
public class IdUtil extends cn.hutool.core.util.IdUtil {
private static final long BASE_DIFF_MILLIS = 1648013774104L;
private static final long BASE_DIFF_MILLIS2 = 1578585779603640320L;
private static final Snowflake snowflake = getSnowflake(1,1);
public static String timeStampId() {
long millis = System.currentTimeMillis();
String prefix = String.format("%013d", millis-BASE_DIFF_MILLIS);
Random random = new Random();
int end2 = random.nextInt(999);
return prefix + String.format("%03d", end2);
}
public static Long timeStampLong() {
return System.currentTimeMillis()-BASE_DIFF_MILLIS;
}
public static String dateTimeStampId() {
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String localTime = df.format(localDateTime);
Random random = new Random();
int end2 = random.nextInt(999);
return localTime + String.format("%03d", end2);
}
public static String dateTimeId() {
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String localTime = df.format(localDateTime);
return localTime;
}
public static String genCode(int len) {
int max = new Double(Math.pow(10, len)).intValue() - 1;
Random random = new Random();
int end2 = random.nextInt(max);
return String.format("%0"+len+"d", end2);
}
public static long getSnowFlakeId(){
return snowflake.nextId()-BASE_DIFF_MILLIS2;
}
}