平时项目中只要涉及表,那么一定能接触到众多各式各样的ID编号,博主整理一些常用的ID格式,整合一个ID生成工具类,供大家参考,如果有什么不足指出,烦请留言批评指正,尽量改正,感激不尽。
目前包括ID生成规则:
- 根据时间获取编号
- 根据时间+机器码+后面累加的位数组成的编号
- 根据时间+机器码+后面随机数组成的编号
- 根据前缀+累加位数
- 根据前缀+时间
- 根据后缀+累加位数
- 根据后缀+时间
简单的使用方法:
public static void main(String[] args) {
IDUtil idUtil = new IDUtil("yyyyMMddHHmmssSSS");//yyyy-MM-dd HH:mm:ss:SSS
for (int i = 0; i < 10; i++) {
System.out.println(idUtil.generatedNoByFormatDateAndRandom(5,"5"));
}
}
时间格式在创建对象时传入。在集群中使用应传入机器码或唯一标识作为唯一ID的根据。
更新
添加了多个可选的构造方法,来适应不同的生成规则。
IDUtil:
public class IDUtil {
private SimpleDateFormat sfd = new SimpleDateFormat("yyyyMMddHHmmssSSS");
private Set<Integer> randomSet = new HashSet<Integer>();
private String dateFlag;
private int sequence;
public IDUtil() {
this.sfd = new SimpleDateFormat("yyyyMMddHHmmssSSS");
this.dateFlag = this.sfd.format(new Date());
this.sequence = 0;
}
public IDUtil(String format) {
this.sfd = new SimpleDateFormat(format);
this.dateFlag = this.sfd.format(new Date());
this.sequence = 0;
}
public IDUtil(int sequence) {
this.sequence = sequence;
}
public String generatedNoByFormatDate(){
return this.generatedNoByFormatDate(null);
}
public String generatedNoByFormatDateAndNum(int digits){
return this.generatedNoByFormatDateAndNum(digits, null);
}
public String generatedNoByFormatDateAndRandom(int randomDigits){
return this.generatedNoByFormatDateAndRandom(randomDigits, null);
}
public String generatedNoByPrefix(String prefix, int digits){
return this.generatedNoByPrefix(prefix, digits, null);
}
public String generatedNoByDateAndPrefix(String prefix){
return this.generatedNoByDateAndPrefix(prefix, null);
}
public String generatedNoBySuffix(String suffix, int digits){
return this.generatedNoBySuffix(suffix, digits, null);
}
public String generatedNoByDateAndSuffix(String suffix){
return this.generatedNoByDateAndSuffix(suffix, null);
}
public String generatedNoByFormatDate(String machine){
String formatDate = this.sfd.format(new Date());
if (Objects.nonNull(machine)){
return formatDate + machine;
}
return formatDate;
}
public String generatedNoByFormatDateAndNum(int digits, String machine){
String no = "";
String formatDate = this.sfd.format(new Date());
if (this.dateFlag.equals(formatDate)){
this.sequence ++;
}else {
this.sequence = 0;
}
if (Objects.nonNull(machine)){
no = formatDate + machine + String.format("%0"+digits+"d", this.sequence);
}else {
no = formatDate + String.format("%0"+digits+"d", this.sequence);
}
return no;
}
public String generatedNoByFormatDateAndRandom(int randomDigits, String machine){
String no = "";
String formatDate = this.sfd.format(new Date());
StringBuffer sb = new StringBuffer("9");
for (int i = 1; i < randomDigits; i++) {
sb.append("9");
}
Random random = new Random();
int randomNum = 0;
do {
randomNum = random.nextInt(Integer.valueOf(sb.toString()));
}while (this.randomSet.contains(randomNum));
randomSet.add(randomNum);
if (Objects.nonNull(machine)){
no = formatDate + machine + String.format("%0"+randomDigits+"d", randomNum);
}else {
no = formatDate + String.format("%0"+randomDigits+"d", randomNum);
}
return no;
}
public String generatedNoByPrefix(String prefix, int digits, String machine){
String no = "";
this.sequence ++;
if (Objects.nonNull(machine)){
no = prefix + machine + String.format("%0"+digits+"d", this.sequence);
}else {
no = prefix + String.format("%0"+digits+"d", this.sequence);
}
return no;
}
public String generatedNoByDateAndPrefix(String prefix, String machine){
String no = "";
String formatDate = this.sfd.format(new Date());
if (Objects.nonNull(machine)){
no = prefix + formatDate + machine;
}else {
no = prefix + formatDate;
}
return no;
}
public String generatedNoBySuffix(String suffix, int digits, String machine){
String no = "";
this.sequence ++;
if (Objects.nonNull(machine)){
no = machine + String.format("%0"+digits+"d", this.sequence) + suffix;
}else {
no = String.format("%0"+digits+"d", this.sequence) + suffix;
}
return no;
}
public String generatedNoByDateAndSuffix(String suffix, String machine){
String no = "";
String formatDate = this.sfd.format(new Date());
if (Objects.nonNull(machine)){
no = formatDate + machine + suffix;
}else {
no = formatDate + suffix;
}
return no;
}
}
到此这篇关于利用Java自写一个生成ID的工具类的文章就介绍到这了,更多相关Java生成ID工具类内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!