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订单号(时间加流水号)的更多相关文章

  1. Java订单号生成,唯一订单号(日均千万级别不重复)

    Java订单号生成,唯一订单号 相信大家都可以搜索到很多的订单的生成方式,不懂的直接百度.. 1.订单号需要具备以下几个特点. 1.1 全站唯一性. 1.2 最好可读性. 1.3 随机性,不能重复,同 ...

  2. JAVA获取当前时间加一天

    01.获取当前时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.for ...

  3. Java 中 日期 时间 加减

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //方法1(推荐,功能强大灵活多变) Ca ...

  4. Java 得到指定时间加半个小时之后得时间

    Calendar c = Calendar.getInstance(); c.setTime(cur); //设置时间 c.add(Calendar.MINUTE, 1); //日期分钟加1,Cale ...

  5. 订单号生成逻辑,C#和JAVA双版

    五年没写过博客了,倒是天天在看 转来转去,又转回技术 原来一直在使用微软爸爸的东西,最近一两年开始玩android,玩java,还有PostgreSQL 都有些应用了,倒是可以整理些随笔出来,这就是其 ...

  6. JAVA时间进行比较和转换,时间加减得到天数

    转自:https://blog.csdn.net/iteye_8535/article/details/82246006 JAVA时间进行比较和转换,时间加减得到天数 1. 把时间类型的字符串转为DA ...

  7. Java代码生成16位纯数字的订单号

    //生成16位唯一性的订单号 public static void getUUID(){ //随机生成一位整数 int random = (int) (Math.random()*9+1); Stri ...

  8. 全局唯一订单号生成方法(参考snowflake)

    backgroud Snowflake is a network service for generating unique ID numbers at high scale with some si ...

  9. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

随机推荐

  1. linux中的cd ..和cd -命令有什么区别?

    cd ..是返回上一层目录, cd -是返回到上一次的工作目录,如果当前目录是/执行cd /usr/local再执行cd ..就是到 /usr而执行cd -就是到/

  2. delphi读写文本文件

    delphi读写文本文件   在工作中遇到了这样一个问题,使用PLSQL将一个表的数据转化成一些列的insert语句存储到一个.sql文本中,我本来想使用access数据库中的查询视图一次执行这些语句 ...

  3. 解读sample3

    说明 理解被测试代码 理解测试代码:test fixture简介 编写fixture class TEST_F宏 其他 不应该被忽略的注释 说明 被测试代码文件 sample3-inl.h 测试代码文 ...

  4. Centos6.x/Oracle11G 自动化静默安装配置脚本

    部分脚本截图如下,要想玩转联系Ruiy哥提供脚本下载路径,附件在本博客的文件栏中维护,为了避免懒人一味的索取别人的劳动成果特此如此; 想玩的Ruiy mail to you! 快6.1了,6.1娃子们 ...

  5. QThread 与 QObject的关系

    Threads and QObjects QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信 ...

  6. 【错误总结之(一)】error LNK2038: 检測到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”

    1>cvblob.lib(cvblob.obj) : error LNK2038: 检測到"_ITERATOR_DEBUG_LEVEL"的不匹配项: 值"0&quo ...

  7. [Javascript] Refactoring: Polymorphic Functions

    if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions ...

  8. C语言判断文件是否存在

      用函数access,头文件是io.h,原型:    int   access(const   char   *filename,   int   amode); amode参数为0时表示检查文件的 ...

  9. Java基础知识强化81:Math类random()方法之获取任意范围的随机数案例(面试题)

    1. 需求:设计一个方法,可以实现获取任意范围内的随机数 分析:使用方法random()如下: public static double random() 注:Returns a pseudo-ran ...

  10. Android(java)学习笔记254:ContentProvider使用之内容观察者(观察发出去的短信)

    1.新建一个案例如下: 2. 不需要添加权限,同时这里布局文件不做修改,来到MainActivity,如下: package com.itheima.sendsmslistener; import a ...