How to increment serial number?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rawringbuny
    New Member
    • Jan 2011
    • 2

    How to increment serial number?

    hi! i was doing wondering if there's a way to print out & increment 3-digit-serial no.?
    i was planning to print out an invoice.
    i want the starting of the invoice to be in this format YYYYMMDDNNN (where YYYY is the year, MM is month, DD is date & NNN is serial no (e.g: 001, 002, 003....) )

    i've did the Year, month and date already. but when i want to print out the serial no. , it shows "1, 2 ..." instead of "001, 002"..

    can someone help me?

    this is the code i did so far.


    Code:
    package JPRG;
    import java.util.Calendar;
    
    public class ShoppingTransactionRecord {
    
        public static void main (String args[]){
            
          int serialNo = 000;
     
          Calendar cal = Calendar.getInstance();
     
          int date = cal.get(Calendar.DATE);
          int month = cal.get(Calendar.MONTH) + 1;
          int year = cal.get(Calendar.YEAR);
          
    
          for (int i=0;i<10;i++){
              serialNo++;
        System.out.println("Testing =====> " + year + month + date + serialNo);
          }
       }
     
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you can use Java formatting



    e.g.
    Code:
        System.out.printf("Testing =====> %d%d%d%03d\n",  year , month , date , serialNo);
    gives
    Code:
    Testing =====> 2011124001
    Testing =====> 2011124002
    Testing =====> 2011124003
    Testing =====> 2011124004
    Testing =====> 2011124005
    Testing =====> 2011124006
    Testing =====> 2011124007
    Testing =====> 2011124008
    Testing =====> 2011124009
    Testing =====> 2011124010
    you can set the field width for the other parameters to ensure the output is what you require

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      rawringbuny,

      I think you want to use the static method String.format, with a format similar to the one in the example from horace1.

      Luck!
      Oralloy

      Comment

      Working...