當前位置:首頁 » 編程語言 » java計時

java計時

發布時間: 2022-01-10 05:06:36

『壹』 java計時代碼,具體如下~

定時器計時->存入全局變數->程序開始->A頁面讀取全局變數時間->跳轉B頁面->B讀取全局變數時間,這樣沒誤差

『貳』 JAVA中如何使用計時函數

Java計時函數currentTimeMills()

System.currentTimeMills()計時精確到毫秒級,跟計算機以1970年1月1日0時為計時起點一樣,該函數方法統計的也是從1970年1月1日0時開始,到程序運行到該函數時刻的毫秒總數。

該函數方法定義在Java系統類System中,如果想實現程序運行計時功能也很簡單,只要在程序前後分別放置該函數方法,然後後減前毫秒總數,就能計算程序運行的耗時。具體實現如下:

long startTime = System.currentTimeMills(); //程序開始記錄時間

//。。。 。。。

long endTime = System.currentTimeMills(); //程序結束記錄時間

long TotalTime = endTime - startTime; //總消耗時間

『叄』 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 時間現格式為00:00:00開始計時,如何表示

import java.awt.FlowLayout;
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.JLabel;
import javax.swing.Timer;

public class Test extends JFrame{

JLabel lbl=new JLabel();
Date now=new Date();
public Test() {

now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);

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");

lbl.setText(formatter.format(now));
}

});
timer.start();

this.setLayout(new FlowLayout());
this.add(lbl);

this.setSize(300,200);
this.setVisible(true);
}
public static void main(String[] args) {
Test t=new Test();
}
}

『伍』 用JAVA編寫計時器

計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器

{

publicstaticvoidmain(String[]args){
newCalculagraph().start();
}

privatelongnow=0l;

privatelongstart=System.currentTimeMillis();//程序啟動時間的毫秒值

privatelongtime;

publicvoidrun(){
while(true){
now=System.currentTimeMillis();//獲取一秒之後的毫秒值
time=now-start;//兩個時間相減的到毫秒差
System.out.format("%02d:%02d:%02d ",
time/(1000*60*60)%60/*時*/,
time/(1000*60)%60/*分*/,
time/1000%60/*秒*/);//格式化字元串輸出
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}

『陸』 JAVA計時器

/** 每3秒運行一次 */
Timer timer = new Timer();
TimerTask tt = new TimerTask() {

public void run() {
/* 投放炸彈的操作 */

new Thread() {

public void run() {
try {
Thread.sleep(5000);
}catch (Exception e) { }
/* 爆炸的操作 */
}

}.start();

}

}; timer.schele(tt, 0, 3000);

『柒』 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需要一條秒錶計時器代碼

好吧,已看到你的評論,我在這里再回答一次:

1)你所說的置頂如果是屬於懸浮窗效果,那麼JFrame實例化後,再添加一行如下的代碼:

form1.setAlwaysOnTop(true);//總是允許窗口置頂

2)時分秒更簡單了,除一除轉轉換就行了,沒有技術含量。

3)快捷鍵通過JButton類的setMnemonic方法實現

So,綜上,整個程序的實現演算法如下:

packagehky.example;

importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.WindowEvent;
importjava.awt.event.WindowListener;
importjava.io.*;
importjava.util.*;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importjavax.swing.plaf.OptionPaneUI;


publicclassDemo{
staticbooleanisRuning=false;
staticbooleanisFirst=true;
staticIntegerhmsCounter=0;
staticinthour,minute,second;

@SuppressWarnings("unchecked")
publicstaticvoidmain(String[]args)throwsException{

JFrameform1=newJFrame("Form1");
form1.setAlwaysOnTop(true);//1)總是允許窗口置頂

JTextFieldjTextField=newJTextField(10);
jTextField.setSize(10,10);
jTextField.setText("0");
jTextField.setEditable(false);

JButtonjButton=newJButton("開始");
jButton.setSize(10,10);


Threadthread=newThread(newRunnable(){
@Override
publicvoidrun(){
while(true){
while(isRuning){
++hmsCounter;
//3)時分秒顯示
hour=hmsCounter/3600;
minute=hmsCounter%3600/60;
second=hmsCounter%60;
jTextField.setText(hour+"時"+minute+"分"+second+"秒");
try{Thread.sleep(1000);}catch(Exceptione2){}
}
try{Thread.sleep(200);}catch(Exceptione2){}//修復上一次回答的版本可能會存在的Bug
}
}
});

jButton.setMnemonic(KeyEvent.VK_ENTER);//2)給JButton發送Alt+Enter快捷鍵
jButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
Stringtext=jButton.getText().equals("開始")?"暫停":"開始";
jButton.setText(text);
isRuning=!isRuning;
if(isFirst){
thread.start();
isFirst=false;
}
}
});

JPanelpanel=newJPanel();
panel.setSize(200,200);
panel.add(jTextField,BorderLayout.NORTH);
panel.add(jButton,BorderLayout.CENTER);
form1.add(panel);
form1.setBounds(200,100,250,150);
form1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form1.addWindowListener(newWindowListener(){
@Override
publicvoidwindowOpened(WindowEvente){
//TODOAuto-generatedmethodstub

}

@Override
publicvoidwindowIconified(WindowEvente){
//TODOAuto-generatedmethodstub

}

@Override
publicvoidwindowDeiconified(WindowEvente){
//TODOAuto-generatedmethodstub

}

@Override
publicvoidwindowDeactivated(WindowEvente){
//TODOAuto-generatedmethodstub

}

@Override
publicvoidwindowClosing(WindowEvente){
//窗口關閉前取出文本框的數字保存到外部文件,代碼在此處寫
JOptionPane.showMessageDialog(null,"Areyousureclosing?");
}

@Override
publicvoidwindowClosed(WindowEvente){
//TODOAuto-generatedmethodstub

}

@Override
publicvoidwindowActivated(WindowEvente){
//TODOAuto-generatedmethodstub

}
});

form1.setVisible(true);
}
}

『玖』 如何用java實現一個計時器

java實現一個計時器,可以使用線程的sleep方法,實例如下:


{
Threadxc;
Dao=newDaoImpl();
publicTestDingShi()
{
xc=newThread(this);//線程開啟
xc.start();
}
publicvoidrun()
{
while(true)
{
try
{
xc.sleep(1000);//睡眠開始計時
}
catch(InterruptedExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//TODO定時在此

}

}
}

『拾』 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");
}
}

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

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:765
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:664
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:311
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:289
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:817
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:162
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:94
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:507
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:658
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:481