2011-11-01 105 views
-1

我想顯示系統時間(不是日期)到JLabel中。我可以使用這個日期進行比較嗎?如何在Netbeans中獲取系統時間?

背景:我正在開發一個Netbeans Java項目 - Multiplex Ticket Booking System。 如果電影已經開始,用戶應該無法預訂門票。

我不知道核心Java。 所以,請讓答案冗長而清晰,以便即使是新手也能理解。

回答

1
Calendar cal = Calendar.getInstance(); 
cal.getTime(); 
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
time.setText(sdf.format(cal.getTime())); 

時間--->的JLabel

0

名稱來顯示當前的時間,你可以使用

Date d = new Date(); 
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
    jLabel1.setText("The current time is "+sdf.format(d)); 

要找到電影是否已啓動或沒有:

我們可以將電影的開始時間存儲在日期數據類型中,並創建一個函數啓動電影作爲輸入的時間,並與當前的日期進行比較,以發現是否可以預訂票對電影或不

boolean Has_movie_started(Date movie_start_date){ 
     //gets the current time 
     Date current_time = new Date(); 
     //calculates the difference in Milliseconds between start of movie and current time 

     long difference_milliseconds = movie_start_date.getTime()-current_time.getTime(); 

     //getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT 


     boolean movie_started; 

     // if the difference is positive that means that movie has much more milliseconds and then current date. 

      if(difference_milliseconds>0){ 
       movie_started = false; 
      } 
      else{ 
       movie_started = true; 
      } 

     return movie_started; 
    } 

如果電影是沒有其他人開始真正被電影已經開始這個函數返回false。 您可以使用它來確定該電影的票證是否可以預訂。 :)