Java:SimpleDateFormat时间戳不更新

时间:2012-03-10 03:30:52

标签: java timestamp simulation priority-queue simpledateformat

晚上, 我正在尝试使用以下SimpleDate格式为实体添加到我的PriorityQueue时创建时间戳:[yyyy / MM / dd - hh:mm:ss a](以下结果示例) 纳秒精度不是100%必要

  

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

您知道如何维护“已用时间”时间戳,以显示客户何时被添加到PriorityQueue中?

在我遇到的StackOverflow线程中,大多数人说使用System.nanoTime();虽然我无法在线找到资源将其实现为SimpleDateFormat。我也咨询了同事。

另外,我为没有使用语法高亮显示道歉(如果S.O支持它)

  

代码摘录[省略未使用的方法]:

 <!-- 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

问题:

  • 时间戳不更新,仅代表初始运行时(见下文)
    • 延迟1-4秒w / Thread.sleep(xxx)(伪随机生成)
    • 问题可能在assignArrivalTime()方法
  

输出:

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)

感谢您的帮助,我希望我的问题足够清楚&amp;我已经充分遵循了您的格式指南。

1 个答案:

答案 0 :(得分:1)

每次都必须使用Date的新实例来获取最新的时间戳。

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

尝试在上一行中将date替换为new Date()

相关问题