計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
創(chuàng)新互聯(lián)建站是一家網(wǎng)站設計公司,集創(chuàng)意、互聯(lián)網(wǎng)應用、軟件技術為一體的創(chuàng)意網(wǎng)站建設服務商,主營產品:成都響應式網(wǎng)站建設、品牌網(wǎng)站設計、全網(wǎng)營銷推廣。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡互動的體驗,以及在手機等移動端的優(yōu)質呈現(xiàn)。網(wǎng)站建設、做網(wǎng)站、移動互聯(lián)產品、網(wǎng)絡運營、VI設計、云產品.運維為核心業(yè)務。為用戶提供一站式解決方案,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價值服務。
public?class?Calculagraph?extends?Thread?{
public?static?void?main(String[]?args)?{
new?Calculagraph().start();
}
private?long?now?=?0l;
private?long?start?=?System.currentTimeMillis();//?程序啟動時間的毫秒值
private?long?time;
public?void?run()?{
while?(true)?{
now?=?System.currentTimeMillis();//?獲取一秒之后的毫秒值
time?=?now?-?start;//?兩個時間相減的到毫秒差
System.out.format("%02d:%02d:%02d\n",
time?/?(1000?*?60?*?60)?%?60/*?時?*/,?
time?/?(1000?*?60)%?60/*?分?*/,?
time?/?1000?%?60/*?秒?*/);//?格式化字符串輸出
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
怎么還沒人回答,看不過去了,用不用多線程根據(jù)你的程序需要,
import java.io.IOException;
import java.util.Timer;
public class TimerTest {
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒后執(zhí)行此任務,每次間隔2秒,如果傳遞一個Data參數(shù),就可以在某個固定的時間執(zhí)行這個任務.
while(true){//這個是用來停止此任務的,否則就一直循環(huán)執(zhí)行此任務了
try {
int ch = System.in.read();
if(ch-'c'==0){
timer.cancel();//使用這個方法退出任務
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static class MyTask extends java.util.TimerTask{
@Override
public void run() {
//你要進行的操作
}
}
}
大概就是這樣了,在根據(jù)你的業(yè)務需要查一下資料,就可以搞定了!
簡單代碼如下:
import?java.awt.Button;
import?java.awt.FlowLayout;
import?java.awt.Label;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?javax.swing.JFrame;
import?javax.swing.Timer;
@SuppressWarnings("serial")
public?class?Timers?extends?JFrame?{
final?Label?lab?=?new?Label();
Date?now?=?new?Date();
@SuppressWarnings("deprecation")
public?Timers()?{
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
setBounds(550,?270,?200,?150);
final?Timer?timer?=?new?Timer(1000,?new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
Date?now2?=?new?Date(now.getTime()?+?1000);
now?=?now2;
SimpleDateFormat?formatter?=?new?SimpleDateFormat("HH:mm:ss");
lab.setText(formatter.format(now));
}
});
Button?b1?=?new?Button("開始");
Button?b2?=?new?Button("停止");
b2.setBounds(40,?40,?40,?40);
b1.setBounds(30,?30,?30,?30);
b1.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
Button?b?=?(Button)?e.getSource();
b.setLabel("開始");
timer.start();
}
});
b2.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
Button?b?=?(Button)?e.getSource();
b.setLabel("停止");
timer.stop();
}
});
this.setLayout(new?FlowLayout());
this.add(b2);
this.add(b1);
this.add(lab);
this.setSize(300,?200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public?static?void?main(String[]?args)?{
Timers?t?=?new?Timers();
t.lab.setText("00:00:00");
}
}
不知是否幫到你,如滿意請采納!
網(wǎng)站名稱:java代碼實現(xiàn)簡單計時 分析java代碼耗時工具
文章鏈接:http://sd-ha.com/article36/doochsg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、企業(yè)建站、營銷型網(wǎng)站建設、企業(yè)網(wǎng)站制作、網(wǎng)站設計公司、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)