package com.shgz.comm.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* ********************************
*
* @author zhaoqingwei.
* 天下英雄出我辈,一入江湖岁月催.
* 20182018/4/2016:27
* ********************************
* 描述:String 数据处理工具包
* <p>
* 注释:
**/
public class StrUtil {
/**
* 生成 ID
* @param prefix ID前缀(大写字母标记)
* @param rl 0 左补齐 1 右补齐
* @param batchno 当前数据库最大ID
* @param addition 补齐内容
* @param batchnoLength 固定长度
* @return
*/
public static String formatId(String prefix,String rl,String batchno,String addition,int batchnoLength){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
StringBuffer strno = new StringBuffer(prefix);
try {
String temp = String.valueOf(Integer.parseInt(batchno.substring(batchno.length()-batchnoLength,batchno.length()))+1);
if ("0".equals(rl)){
for (int i = 0;i<batchnoLength+prefix.length()-strno.length();i++){
temp=addition+temp;
}
}else if ("1".equals(rl)){
for (int i = 0;i<batchnoLength+prefix.length()-strno.length();i++){
temp+=addition;
}
}else{
temp = "";
}
return new String(strno.append(sdf.format(new Date())).append(temp));
}catch (Exception e){
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
System.out.println(formatId("HT","1","HT20180217101511000002","0",6));
System.out.println(formatId("HT","0","HT20180217101511000002","0",6));
}
}
结果展示如下:生成规则 前缀+时间+6位数(6位数实现左右补齐)
HT201804200618253000000
HT201804200618250000003
Process finished with exit code 0
Java实现数据自动按规则补齐
最新推荐文章于 2024-08-31 13:27:22 发布