我现在正在做一个有关单据的管理系统
要求那个单据号要根据系统的时间来自动编号
比如今天的编号就是20080918001这样
日期我取出来没啥问题
怎样才能让后面加上3个零,然后自动加一呢
请各位帮忙看看

解决方案 »

  1.   

    import java.text.DecimalFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class Test01 {
        
        public static void main(String[] args) {
            Date date = new Date();
            int k = 1;
            
            // JDK 1.5 or higher
            String number = String.format("%tY%<tm%<td%03d", date, k);
            System.out.println(number);
            
            // JDK 1.4 or lower
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            DecimalFormat df = new DecimalFormat("000");
            String d = sdf.format(date);
            String n = df.format(k);
            System.out.println(d + n);
        }
    }
      

  2.   

    晕,忘记说话了.呵呵.
    火龙果的这个方法是没有问题的.但是问题是不能完全满足楼主的需求.
    我的意见是:
    1.把自增的序列号,保存在一个地方(如一个文本文件中,Session中,或者数据库中).
    当然如果你的数据库正好要存这个字段,那么就是在生产之前读取一下,然后取后三位+1;
    这个过程一定要是同步的.(你的方法要同步,不然有可能存入的一样的值)2.然后再按照火龙果的方法生成.其实我本人一直用的方法是:("000"+num).subString(String.valueOf(num).length());
      

  3.   

    采用文件作为持久存储做了一个,可以参考一下,有需要的话可以改成数据库中的。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);
        }
        
        /**
         * 获得序列号,同时更新持久化存储中的序列
         * @param current 当前的日期
         * @param start   初始化的序号
         * @return 所获得新的序列号
         */
        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;
        }
    }
      

  4.   

    用读取文件,修改文件的方法 import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;public class TestID { /**
     * 取单号 如果日期和现在日期不符合,改为现在日期加001
     * 
     * @param key
     * @return
     */
    public static String getID(String key) {
    String ID = null;
    Properties pro = new Properties();
    File file = new File("ID.properties");
    try {
    pro.load(new FileInputStream(file));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    ID = pro.getProperty(key);
    String subID = ID.substring(2, 10); Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String dateNowStr = sdf.format(date); if (!dateNowStr.equals(subID)) {
    ID = key.substring(0, 2) + dateNowStr + "-8001";
    pro.setProperty(key, ID);
    try {
    pro.store(new FileOutputStream(file), "num1");
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } } return ID;
    } /**
     * 执行完操作 增加交易单后将 号码加1
     * 
     * @param key
     */ public static void addIDandOne(String key) {
    String ID = null;
    Properties pro = new Properties();
    File file = new File("ID.properties");
    try {
    pro.load(new FileInputStream(file));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } // Date date = new Date();
    // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    // String dateNowStr = sdf.format(date); ID = pro.getProperty(key); String dateNowStr = ID.substring(2, 10);
    ID = String.valueOf(Integer.parseInt(ID.substring(11)) + 1);
    ID = key.subSequence(0, 2) + dateNowStr + "-" + ID;
    pro.setProperty(key, ID);
    try {
    pro.store(new FileOutputStream(file), "num");
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    System.out.println(TestID.getID("HYNumber"));
    TestID.addIDandOne("HYNumber");
    // System.out.println(Integer.parseInt("20080828001"));
    }
    }
      

  5.   

    我在做的一个小系统也正采用这样的方法作为单号。
    目前还是用手工输入的,看来是可以考虑自动的生成啊。
    import java.text.SimpleDateFormat
    //...
    Date date=new Date();
    SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
    BufferString str = format.format(date).toString();
    str=str.apend("000");//直接String str那当然就 str+="000";
    //...做个计数器counter
    把str转为long同counter相加,不知道可不可以。