zoukankan      html  css  js  c++  java
  • 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,可以自行创建这样的文件,然后放到项目的根目录下就可以。此文件主要是用于记录上一次执行程序的节点。

     

  • 相关阅读:
    Ubuntu系统Anaconda安装Pytorch,教你如何优雅的安装环境(1-5)
    优雅的装系统------安装MacOS 10.13.5系统教程
    关闭Ubuntu系统更新方法
    毫秒钟搞定anaconda环境使用清华镜像安装OpenCV,教你如何优雅的安装环境(1-4)
    Ubuntu16.04安装Anaconda3并配置国内镜像,教你优雅的安装环境(1-3)
    Anaconda常用命令
    Ubuntu16.04安装CUDA10.2+cuDNN7.6.5(福利区),教你如何优雅的安装环境(1-2)
    Ubuntu16.04安装NVIDIA驱动,教你如何优雅的安装环境(1-1)
    简述 QPS、TPS、并发用户数、吞吐量关系
    Springboot验证注解@vaild的使用
  • 原文地址:https://www.cnblogs.com/zhang-cb/p/6112574.html
Copyright © 2011-2022 走看看