【问题标题】:Java: SimpleDateFormat timestamp not updatingJava:SimpleDateFormat 时间戳未更新
【发布时间】:2012-03-10 03:30:52
【问题描述】:

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

1: 2012/03/09 - 09:58:36 下午

您知道如何维护“已用时间”时间戳,以显示客户何时被添加到 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

问题:

  • 时间戳不更新,仅代表初始运行时间(见下文)
    • 使用 Thread.sleep(xxx) 延迟 1-4 秒(伪随机生成)
    • 问题可能出在 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)

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

【问题讨论】:

  • Date 不会自动更新。尝试改用timeStamp.add(....format(new Date()));

标签: java timestamp simulation priority-queue simpledateformat


【解决方案1】:

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

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

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

【讨论】:

  • 我不介意 :) 但我在发帖后看到了你的评论 :)
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多