Java: SimpleDateFormat timestamp not updating -


evening, i'm trying create timestamp when entity added priorityqueue using following simpledate format: [yyyy/mm/dd - hh:mm:ss a] (samples of results below) nano-second precision not 100% necessary

1: 2012/03/09 - 09:58:36 pm

do know how can maintain 'elapsed time' timestamp shows when customers have been added priorityqueue?

in stackoverflow threads i've come across, use system.nanotime(); although can't find resources online implement simpledateformat. have consulted colleagues.

also, apologize not using syntax highlighting (if s.o supports it)

code excerpt [unused methods omitted]:

 <!-- language: java -->  package grocerystoresimulation;  /*****************************************************************************  * @import  */  import java.util.priorityqueue;  import java.util.random;  import java.util.arraylist;  import java.util.date;  import java.text.dateformat;  import java.text.simpledateformat;  /************************************************************************************  public class grocerystoresimulation {  /************************************************************************************  * @fields   */     private priorityqueue<integer> pq = new priorityqueue<integer>();     private random rand = new random(); //instantiate new random object      private date date = new date();     private dateformat dateformat = new simpledateformat("yyyy/mm/dd - hh:mm:ss a");      private arraylist<string> timestamp = new arraylist<string>(); //store timestamps      private int customersserved; //# of customers served during simulation /************************************************************************************  * @constuctor  */     public grocerystoresimulation(){         system.out.println("instantiated new grocerystoresimulation @ ["                 + dateformat.format(date) + "]\n" + insertdivider());          //program body         while(true){             try{                 thread.sleep(generatewaittime());                  newcustomer(customersserved);             } catch(interruptedexception e){/*catch 'em all*/}         }     }  /************************************************************************************  * @param string id  */     private void newcustomer(int id){        system.out.println("customer # " + customersserved + " added queue. . .");        pq.offer(id); //insert element priorityqueue        customersserved++;        assignarrivaltime(id); //call assignarrivaltime() method    } //newcustomer() /************************************************************************************  * @param string id   */    private void assignarrivaltime(int id){        timestamp.add(id + ": " + dateformat.format(date));        system.out.println(timestamp.get(customersserved-1));    } //assignarrivaltime() /************************************************************************************   * @return int   */    private int generatewaittime(){        //local variables        int low = 1000;  //1000ms        int high = 4000; //4000ms        int waittime = rand.nextint(high-low) + low;        system.out.println("delaying for: " + waittime);         return waittime;    } //***********************************************************************************    private static string insertdivider(){        return ("******************************************************************");    } //*********************************************************************************** } //grocerystoresimulation 

problem:

  • timestamp not update, represents initial runtime (see below)
    • delaying 1-4 seconds w/thread.sleep(xxx) (pseudo-randomly generated)
    • problem may in assignarrivaltime() method

output:

run: instantiated new grocerystoresimulation @ [2012/03/09 - 09:58:36 pm] ****************************************************************** delaying for: 1697 customer # 0 added queue. . . 0: 2012/03/09 - 09:58:36 pm delaying for: 3550 customer # 1 added queue. . . 1: 2012/03/09 - 09:58:36 pm delaying for: 2009 customer # 2 added queue. . . 2: 2012/03/09 - 09:58:36 pm delaying for: 1925 build stopped (total time: 8 seconds) 

thank assistance, hope question clear enough & i`ve followed formatting guidelines sufficiently.

you have use new instance of date everytime recent timestamp.

private void assignarrivaltime(int id){     timestamp.add(id + ": " + dateformat.format(date));  ------------------------------------------------^^^^ 

try replacing date new date() in above line.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -