public class CodeUtil { private static int width=4; //位数 private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); private static DecimalFormat df = null; private static final Logger log=Logger.getLogger(CodeUtil.class); public static synchronized String getNumber() { String result = null; try { File file=new File("SNumber.dat"); char[] chs = new char[width]; for (int i = 0; i < width; i++) { chs[i] = '0'; } df = new DecimalFormat(new String(chs)); Date current = new Date(); String date = sdf.format(current); int num = 1; if (file.exists()) { List< String > list = FileUtil.readList(file); String[] data = list.get(0).split(","); if (date.equals(data[0])) { num = Integer.parseInt(data[1]); } } FileUtil.rewrite(file, date + "," + (num + 1)); result = sdf.format(current) + df.format(num); } catch (Exception e) { log.error("生成id有误"); e.printStackTrace(); } return result; } }
结果:201712140001,201712140002,201712140003........
源码下载地址:http://download.csdn.net/download/hua_faded/10158850