Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:45:13+00:00 2026-05-31T01:45:13+00:00

Evening, I’m trying to create a timestamp for when an entity is added to

  • 0

Evening,
I’m trying to create a timestamp for when an entity is added to my PriorityQueue using the 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 you know how I can maintain an ‘elapsed time’ timestamp that shows when customers have been added to the PriorityQueue?

In the StackOverflow threads I’ve come across, most say to use System.nanoTime(); although I can’t find resources online to implement this into a SimpleDateFormat. I have also consulted with colleagues.

Also, I apologize for 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 to queue. . .");
       pq.offer(ID); //insert element into 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 does not update, only represents initial runtime (see below)
    • Delaying by 1-4 seconds w/Thread.sleep(xxx) (pseudo-randomly generated)
    • Problem may be in the assignArrivalTime() method

Output:

run:
Instantiated new GroceryStoreSimulation @ [2012/03/09 - 09:58:36 PM]
******************************************************************
Delaying for: 1697
Customer # 0 added to queue. . .
0: 2012/03/09 - 09:58:36 PM
Delaying for: 3550
Customer # 1 added to queue. . .
1: 2012/03/09 - 09:58:36 PM
Delaying for: 2009
Customer # 2 added to queue. . .
2: 2012/03/09 - 09:58:36 PM
Delaying for: 1925
BUILD STOPPED (total time: 8 seconds)

Thank you for your assistance, I hope my question is clear enough & I`ve followed your formatting guidelines sufficiently.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:45:14+00:00Added an answer on May 31, 2026 at 1:45 am

    You have to use a new instance of Date everytime to get most recent timestamp.

    private void assignArrivalTime(int ID){
        timeStamp.add(ID + ": " + dateFormat.format(date)); 
    ------------------------------------------------^^^^
    

    Try replacing date by new Date() in above line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Evening All, I've created the following stored procedure: CREATE PROCEDURE AddQuote -- Add
Good evening. For a project, I have to create a system. In this system,
Good evening guys, I'm trying to pass multiple checkbox values through AJAX and process
Evening front end wizards. I'm trying to do something that should be quite simple:
Evening all. I have the following code that I need looking into - basically
Evening everyone, I'm using glMultMatrixf in OpenGL to rotate my scene using the matrix:
Evening, Trying to follow the Django tutorial, as seen here, https://docs.djangoproject.com/en/dev/intro/tutorial01/ I run the
Good evening, experts I want to solve recurrence equation using mathematica, x(n) = x(n
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
Good Evening, I'm using Drupal 6, CCK Module, and the Link Field Type. All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.