Java 生成唯一订单号:使用时间戳获取 int 类型订单 ID
可以使用以下代码生成 int 类型的订单号:
import java.util.Date;
public class OrderGenerator {
private static int id = 0;
public static int generate() {
long now = new Date().getTime();
id = (int) (now % 1000000000);
return id;
}
}
在上面的代码中,我们使用了当前时间戳来生成订单号。我们首先获取当前时间戳,然后将其转换为 int 类型,并取模 1000000000,以确保生成的订单号不超过 9 位数。最后,我们将生成的订单号存储在静态变量 id 中,以便下一次调用时可以保证唯一性。
原文地址: https://www.cveoy.top/t/topic/oguq 著作权归作者所有。请勿转载和采集!