Java订单号(时间加流水号)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; public class Test01 { public static void main(String[] args) throws InterruptedException {
SerialNumber serial = new FileEveryDaySerialNumber(5, "EveryDaySerialNumber.dat");
while (true) {
System.out.println(serial.getSerialNumber());
TimeUnit.SECONDS.sleep(2);
}
}
} abstract class SerialNumber {
public synchronized String getSerialNumber() {
return process();
} protected abstract String process();
} abstract class EveryDaySerialNumber extends SerialNumber { protected final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
protected DecimalFormat df = null; public EveryDaySerialNumber(int width) {
if (width < 1) {
throw new IllegalArgumentException("Parameter length must be great than 1!");
}
char[] chs = new char[width];
for (int i = 0; i < width; i++) {
chs[i] = '0';
}
df = new DecimalFormat(new String(chs));
} protected String process() {
Date date = new Date();
int n = getOrUpdateNumber(date, 1);
return format(date) + format(n);
} protected String format(Date date) {
return sdf.format(date);
} protected String format(int num) {
return df.format(num);
} protected abstract int getOrUpdateNumber(Date current, int start);
} class FileEveryDaySerialNumber extends EveryDaySerialNumber { private File file = null; private final static String FIELD_SEPARATOR = ","; public FileEveryDaySerialNumber(int width, String filename) {
super(width);
file = new File(filename);
} @Override
protected int getOrUpdateNumber(Date current, int start) {
String date = format(current);
int num = start;
if (file.exists()) {
List<String> list = FileUtil.readList(file);
String[] data = list.get(0).split(FIELD_SEPARATOR);
if (date.equals(data[0])) {
num = Integer.parseInt(data[1]);
}
}
FileUtil.rewrite(file, date + FIELD_SEPARATOR + (num + 1));
return num;
}
} class FileUtil {
public static void rewrite(File file, String data) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static List<String> readList(File file) {
BufferedReader br = null;
List<String> data = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(file));
for (String str = null; (str = br.readLine()) != null;) {
data.add(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return data;
}
}
注意:代码中有个叫EveryDaySerialNumber.dat,可以自行创建这样的文件,然后放到项目的根目录下就可以。此文件主要是用于记录上一次执行程序的节点。
Java订单号(时间加流水号)的更多相关文章
- Java订单号生成,唯一订单号(日均千万级别不重复)
Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...
- JAVA获取当前时间加一天
01.获取当前时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.for ...
- Java 中 日期 时间 加减
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //方法1(推荐,功能强大灵活多变) Ca ...
- Java 得到指定时间加半个小时之后得时间
Calendar c = Calendar.getInstance(); c.setTime(cur); //设置时间 c.add(Calendar.MINUTE, 1); //日期分钟加1,Cale ...
- 订单号生成逻辑,C#和JAVA双版
五年没写过博客了,倒是天天在看 转来转去,又转回技术 原来一直在使用微软爸爸的东西,最近一两年开始玩android,玩java,还有PostgreSQL 都有些应用了,倒是可以整理些随笔出来,这就是其 ...
- JAVA时间进行比较和转换,时间加减得到天数
转自:https://blog.csdn.net/iteye_8535/article/details/82246006 JAVA时间进行比较和转换,时间加减得到天数 1. 把时间类型的字符串转为DA ...
- Java代码生成16位纯数字的订单号
//生成16位唯一性的订单号 public static void getUUID(){ //随机生成一位整数 int random = (int) (Math.random()*9+1); Stri ...
- 全局唯一订单号生成方法(参考snowflake)
backgroud Snowflake is a network service for generating unique ID numbers at high scale with some si ...
- JAVA中的时间操作
java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...
随机推荐
- adb Logcat用法
转自: http://blog.csdn.net/tiantianshangcha/article/details/6288537 个人认为有一下几个常用命令: adb logcat -b radio ...
- Java 程序员必须掌握的 Linux 命令
作为一个Java开发人员,有些常用的Linux命令必须掌握.即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令.因为很多服务器上都是Linux系统.所以,要和服 ...
- 黑魔法__attribute__((cleanup))
原文地址:http://blog.sunnyxx.com/2014/09/15/objc-attribute-cleanup/ 编译器属性__attribute__用于向编译器描述特殊的标识.检查或优 ...
- Q - Tour - hdu 3488(最小匹配值)
题意:一个王国有N个城市,M条路,都是有向的,现在可以去旅游,不过走的路只能是环(至少也需要有两个城市),他们保证这些城市之间的路径都是有环构成的,现在至少需要走多少路. 分析:因为是有向图所以,而且 ...
- java中Object相关的几个方法
protected Object clone()创建并返回此对象的一个副本. String toString()返回该对象的字符串表示. boolean equals(Object obj)指 ...
- [置顶] Firefox OS 学习——Gaia 编译分析
Gaia作为用户的接口,也是用户可见部分,一些用户的应用也是安装在这一层,所以研究他是很有必要的,对于像我这样的初学者,最直接的学习方法就是通过修改代码,然后可以看到UI的变化,很直观的观察修改结果. ...
- Something broke! (Error 500)——reviewboard
Something broke! (Error 500) 1.什么时候会出现? 不清楚,出现过几次 2.解决手段及方法: 更改/www_rb/conf/settings_local.py文件,将DEB ...
- Java容器详解
线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.在Java中,容器的类型主要有:List.Set ...
- Spring MVC之LocaleResolver(解析用户区域)
为了让web应用程序支持国际化,必须识别每个用户的首选区域,并根据这个区域显示内容. 在Spring MVC应用程序中,用户的区域是通过区域解析器来识别的,它必须实现LocaleResolver接口. ...
- Microsoft Dynamics CRM4.0编程---说明
Introduction(说明) If your organization has customers, you need a software system to help you manage y ...