From 910c201f0f0b8d4ba699f699d51565fa98825a39 Mon Sep 17 00:00:00 2001 From: lxd <1004405501@qq.com> Date: Thu, 13 Mar 2025 10:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/framework/common/util/date/DateUtils.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java index f745f1d..1cd84a6 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.common.util.date; import cn.hutool.core.date.LocalDateTimeUtil; +import java.security.SecureRandom; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Calendar; @@ -149,9 +150,14 @@ public class DateUtils { public static String generateUniqueCode() { // 获取当前日期和时间 LocalDateTime now = LocalDateTime.now(); - // 定义格式化模式 - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); + // 定义格式化模式,年月日 + 秒 + 毫秒 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddssSSS"); // 格式化当前日期和时间为字符串 - return now.format(formatter); + String timestamp = now.format(formatter); + // 生成随机数 + SecureRandom random = new SecureRandom(); + int randomNumber = random.nextInt(100); // 生成 0 到 99 之间的随机数 + // 结合时间戳和随机数生成唯一码 + return timestamp + String.format("%02d", randomNumber); } }