供小白来使用吧 ,可以借鉴可以学习,这些我都实际测试过,可以直接使用,在使用这些 我尽量都加上注解,这样方便大家根据自己的需求改动。当然有什么不好的地方和需要改的可以联系我(第二个我看到别人来写的,如有原创可和我说,我贴地址)
1、无序生成编号(时间+编号)
public static String getDateAndUUID() {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy");//获取时间格式 时间的完整格式为yyyyMMddHHmmss
String cdtSequen = tempDate.format(new java.util.Date());// 获取当前时间
String s = UUID.randomUUID().toString().toUpperCase();//获取UUID UUID为唯一码,
String rtnstr = cdtSequen+s.substring(0, 4);//时间+uuid拼装 截图UUID的前四位,具体根据自己需求来截取
return rtnstr;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println(getDateAndUUID());
}
2 有序生成(时间+编号)
public static String generateNumber() throws SQLException {
String Orderno = null;
String maxOrderno=“select top 1 xxx from xxxx where xxx= 0 order by id desc ”//查询最新编号 有更好的方法联系我
System.out.println("maxOrderno=" + maxOrderno);
SimpleDateFormat format = new SimpleDateFormat("yyyy"); // 获取时间格式 时间的完整格式为yyyyMMddHHmmss
String uid_pfix = format.format(new Date()); // 组合流水号前一部分,NO+时间字符串,如:NO20160126
System.out.println("time=" + format.format(new Date()));
if (maxOrderno != null && maxOrderno.contains(uid_pfix)) { //
String uid_end =
maxOrderno.substring(4, 8); // 截取字符串最后四位,结果:0001,根据自己的位数来截,不然会报出超出范围
System.out.println("uid_end=" + uid_end);
int endNum = Integer.parseInt(uid_end); // 把String类型的0001转化为int类型的1
System.out.println("endNum=" + endNum);
int tmpNum = 10000 + endNum + 1; //编号来加
System.out.println("tmpNum=" + tmpNum);
Orderno = uid_pfix + subStr("" + tmpNum, 1);//最后的拼装
} else {
Orderno = uid_pfix + "0001";
}
return Orderno;
}