/**
*生成订单号、编码工具类
*/
@Component("GenerateNum")
public class GenerateNum {
// 使用单例模式,不允许直接创建实例
private GenerateNum() {
}
// 创建一个空实例对象,类需要用的时候才赋值
private static GenerateNum g = null;
// 单例模式--懒汉模式
public static synchronized GenerateNum getInstance() {
if (g == null) {
g = new GenerateNum();
}
return g;
}
// 全局自增数
private static int count = 0;
// 每毫秒秒最多生成多少订单(最好是像9999这种准备进位的值)
private static final int total = 9999;
// 格式化的时间字符串
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// 获取当前时间年月日时分秒毫秒字符串
private static String getNowDateStr() {
return sdf.format(new Date());
}
// 记录上一次的时间,用来判断是否需要递增全局数
private static String now = null;
/**
* 设置起始值
*
* @param num
*/
public synchronized void reset(Integer num) {
count = num;
now = getNowDateStr();
}
/**
* 生成一个订单号
*/
public synchronized String GenerateOrder() {
String datastr = getNowDateStr();
if (datastr.equals(now)) {
count++;// 自增
} else {
count = 1;
now = datastr;
}
int countInteger = String.valueOf(total).length() - String.valueOf(count).length();// 算补位
String bu = "";// 补字符串
for (int i = 0; i < countInteger; i++) {
bu += "0";
}
bu += String.valueOf(count);
if (count >= total) {
count = 0;
}
return datastr + bu;
}
/**
* 将电话加星号处理
*/
public static String phoneMask(String phone) {
String res = "";
if (!StringUtils.isEmpty(phone)) {
StringBuilder stringBuilder = new StringBuilder(phone);
res = stringBuilder.replace(0, Math.abs(phone.length() - 5), "******").toString();
}
return res;
}
/**
* 将电话加星号处理
*/
public static String nameMask(String name) {
String res = "";
String Str = "";
if (!StringUtils.isEmpty(name)) {
StringBuilder stringBuilder = new StringBuilder(name);
int num = Math.abs(name.length() - 1);//显示1位
for (int i = 0; i < num; i++) {
Str += "*";
}
res = stringBuilder.replace(0, num, Str).toString();
}
return res;
}
/**
* 将字符串前面加星号
*/
public static String starMask(String name, int num) {
String res = "";
String Str = "";
if (!StringUtils.isEmpty(name)) {
StringBuilder stringBuilder = new StringBuilder(name);
for (int i = 0; i < num; i++) {
Str += "*";
}
res = stringBuilder.replace(0, num, Str).toString();
}
return res;
}
/**
* 将字符串后面加星号
*/
public static String endMask(String name, int num) {
String res = "";
String Str = "";
if (!StringUtils.isEmpty(name)) {
StringBuilder stringBuilder = new StringBuilder(name);
for (int i = 0; i < num + 1; i++) {
Str += "*";
}
res = stringBuilder.replace(num, name.length(), Str).toString();
}
return res;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?