當前位置:首頁 » 編程軟體 » java編譯計時代碼

java編譯計時代碼

發布時間: 2023-08-20 04:37:34

java計時

你可以在開始和結束的時候,分別記錄下當前的時間的這毫秒數。然後再減,以下是一段代碼。

public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 當前時間對應的毫秒數
System.out.println("開始 "+startMili);
// 執行一段代碼,求一百萬次隨機值
for(int i=0;i<1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("結束 s"+endMili);
System.out.println("總耗時為:"+(endMili-startMili)+"毫秒");
}
}

㈡ java中如何實現自動計時功能,就是點擊一個start按鈕就開始計時,以秒為單位

簡單代碼如下:

importjava.awt.Button;
importjava.awt.FlowLayout;
importjava.awt.Label;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjavax.swing.JFrame;
importjavax.swing.Timer;

@SuppressWarnings("serial")
{
finalLabellab=newLabel();
Datenow=newDate();
@SuppressWarnings("deprecation")
publicTimers(){
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
setBounds(550,270,200,150);
finalTimertimer=newTimer(1000,newActionListener(){
publicvoidactionPerformed(ActionEvente){
Datenow2=newDate(now.getTime()+1000);
now=now2;
SimpleDateFormatformatter=newSimpleDateFormat("HH:mm:ss");
lab.setText(formatter.format(now));
}
});
Buttonb1=newButton("開始");
Buttonb2=newButton("停止");
b2.setBounds(40,40,40,40);
b1.setBounds(30,30,30,30);
b1.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Buttonb=(Button)e.getSource();
b.setLabel("開始");
timer.start();
}
});
b2.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Buttonb=(Button)e.getSource();
b.setLabel("停止");
timer.stop();
}
});

this.setLayout(newFlowLayout());
this.add(b2);
this.add(b1);
this.add(lab);

this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
Timerst=newTimers();
t.lab.setText("00:00:00");
}
}

不知是否幫到你,如滿意請採納!

熱點內容
編程器資源 發布:2025-03-16 17:59:48 瀏覽:902
加密軟體廠商 發布:2025-03-16 17:59:44 瀏覽:678
魚鉤怎麼樣配置 發布:2025-03-16 17:59:04 瀏覽:156
安卓手機怎麼設置快點 發布:2025-03-16 17:45:35 瀏覽:330
c語言字元串右對齊 發布:2025-03-16 17:42:35 瀏覽:130
充值釣魚源碼 發布:2025-03-16 17:28:16 瀏覽:469
密碼鎖鎖住了怎麼辦 發布:2025-03-16 17:25:51 瀏覽:296
中國移動資料庫 發布:2025-03-16 17:18:14 瀏覽:604
php二維數組函數 發布:2025-03-16 17:16:11 瀏覽:375
安卓系統在哪裡換鴻蒙界面 發布:2025-03-16 17:15:38 瀏覽:865