7 Replies - 687 Views - Last Post: 27 December 2012 - 10:22 PM Rate Topic: -----

#1 stevejohn78636  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-December 12

how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:00 PM

public class CalendarUtil

{

    private Calendar cal = null;

    public String getRemId()

    {

        cal = Calendar.getInstance();

        return "" + cal.get(Calendar.DATE) + (cal.get(Calendar.MONTH)+1) +        cal.get(Calendar.YEAR);

    }        
}



How can we auto generate ID on a button click that will contain the concatenation of date,month,year and a 3 digit counter starting form 000 and display it in a textfield? for eg:- 28122012001, 28122012002, etc and so on. Code that i have been trying is as above

How can we auto generate ID on a button click that will contain the concatenation of date,month,year and a 3 digit counter starting form 000 and display it in a textfield? for eg:- 28122012001, 28122012002, etc and so on. Code that i have been trying is as above

Is This A Good Question/Topic? 0
  • +

Replies To: how can we auto generate ID on a button click and display in textfield

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8379
  • View blog
  • Posts: 31,145
  • Joined: 12-June 08

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:03 PM

Well.. okay.. so that sort of explains it.. but it doesn't explain what is wrong. Are you getting errors? If so what are they and what line? Are you getting an odd output? If so what are you getting?

Side note - I don't think ".DATE" is the same thing as ".DAY".
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,840
  • Joined: 05-April 11

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:14 PM

Quote

date,month,year and a 3 digit counter starting form 000


Uhm date? Don't you mean day ?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:15 PM

You will need a STring format() with %02d if you want the days and months 1 to 9 to appear as 01, 02, 03, 04.. 09

Have a counter in your instance variable that you can increment (using again %03d)
Was This Post Helpful? 0
  • +
  • -

#5 stevejohn78636  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-December 12

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:20 PM

public class CalendarUtil{
    private static String lastUsedDatePrefix = "";
    private static int counter = 0;

    public String getRemId(){
        final String datePrefix = new SimpleDateFormat("ddMMyyy").format(new Date());
        if (lastUsedDatePrefix.equals(datePrefix)) {
            CalendarUtil.counter++;
        }
        else{
            CalendarUtil.lastUsedDatePrefix = datePrefix;
            CalendarUtil.counter = 0;
        }
        final String counterSuffix = ((100 <= CalendarUtil.counter) ? ""
                : (10 <= CalendarUtil.counter) ? "0" : "00")
                + CalendarUtil.counter;
        return datePrefix + counterSuffix;
     }   
}




were should i apply the STring format() with %02d in this code man..?? please help and how can i extract values to a textfield
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 03:26 PM


        cal = Calendar.getInstance();
        String str = String.format("%02d%02d%d%03d", cal.get(Calendar.DAY), 
                                                    (cal.get(Calendar.MONTH)+1), 
                                                     cal.get(Calendar.YEAR), 
                                                     (++counter));
        return str;


and your counter does not have to be static unless you have multiple instance of CalendarUtil
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,612
  • Joined: 27-December 08

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 08:43 PM

Why not just let your database generate a unique id with the PRIMARY KEY and AUTO_INCREMENT constraints?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: how can we auto generate ID on a button click and display in textfield

Posted 27 December 2012 - 10:22 PM

View Postmacosxnerd101, on 27 December 2012 - 11:43 PM, said:

Why not just let your database generate a unique id with the PRIMARY KEY and AUTO_INCREMENT constraints?

Why making things simple ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1