从雪花ID中获取创建时间
发表于:2022-12-13 19:47:10 分类:JAVA 阅读:448次
import java.util.Date; /** * @author ERSREDMA * @date created at 19:32 2022/12/13 * 其中的静态配置需要与ID生成器保持一致 */ public class SnowIdUtil { public static int WORKER_BIT = 5; public static int DATA_CENTER_BIT = 5; public static int SEQ_BIT = 12; public static long EPOCH = 1288834974657L; public static long timestamp(long snowId) { String bid = Long.toBinaryString(snowId); int timestamp = bid.length() - (WORKER_BIT + DATA_CENTER_BIT + SEQ_BIT); String substring = bid.substring(0, timestamp); return Long.parseUnsignedLong(substring, 2); } /** * 根据ID获取生成ID时的真实时间戳 * @param snowId * @return */ public static long getTimestampFromSnowId(long snowId) { return timestamp(snowId) + EPOCH; } public static String getFullTimesFromSnowId(long snowId) { long timestamp = getTimestampFromSnowId(snowId); return LocalDateUtils.parseDate(new Date(timestamp),LocalDateUtils.FULL_TIME_FORMAT); } }
关键词:java,SnowId