Java generates self-increment serial number and re-clear 0 count from the first day of each month (original)

Source: Internet
Author: User

If you do not want to operate the database, here is a way to try, directly on the code:

 PackageCom.seawin.common.util.convert;ImportJava.text.DecimalFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classPrimarygenerater {Private StaticString serial_number = "0001" ; Private StaticPrimarygenerater Primarygenerater =NULL; PrivatePrimarygenerater () {}/*** Get a single implementation of Primarygenerater * *@return   */   Public StaticPrimarygenerater getinstance () {if(Primarygenerater = =NULL) {          synchronized(Primarygenerater.class) {              if(Primarygenerater = =NULL) {Primarygenerater=NewPrimarygenerater (); }          }      }      returnPrimarygenerater; }    /*** Generated date + random number of serial number **/   Publicstring Getnumberforpk () {string id=""; SimpleDateFormat SF=NewSimpleDateFormat ("Yyyymmddhhmmss"); String Temp= Sf.format (NewDate ()); intRandom= (int) (Math.random () *10000); ID=temp+Random; returnID; }        /*** hhtg+ Month +8+0001 * Monthly starting from 0001 count **/ Public Static synchronizedstring GetNumber (String thiscode) {string id=NULL; Date Date=NewDate (); SimpleDateFormat Formatter=NewSimpleDateFormat ("Yymm"); String Thisdata= Thiscode.substring (4, 8); //The judgment is that the last business number that you have taken out of your data is not the current month.      if(!Formatter.format (date). Equals (Thisdata)) {System.out.println ("The New January"); Thisdata=Formatter.format (date); //if it's a new January, it's going to become 0001.id = "HHTG" + thisdata + "80001"; }Else{System.out.println (Month); DecimalFormat DF=NewDecimalFormat ("0000"); //It's not the new January, it's cumulative.id = "HHTG" + formatter.format (date) + "8" + df.format (1 + integer.parseint (thiscode.substring (9, 13))); }    returnID; }   Public Static voidMain (string[] args) {/*** HHTG170980001 * HH company abbreviation + TG Business Type + month + 8 department + 0001 * **/String Sno= "HHTG170980001";      System.out.println (GetNumber (SNO)); }  }  

The results are as follows:

The results of the month replaced are as follows:

Java generates self-increment serial number and re-clear 0 count from the first day of each month (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.