当前位置:首页 » 编程语言 » 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");
}
}

不知是否帮到你,如满意请采纳!

热点内容
上网的账号和密码是什么东西 发布:2024-09-20 16:31:31 浏览:612
安卓手机王者荣耀如何调超高视距 发布:2024-09-20 16:31:30 浏览:428
安卓G是什么app 发布:2024-09-20 16:23:09 浏览:81
iphone怎么压缩文件 发布:2024-09-20 16:08:18 浏览:356
linux查看用户名密码是什么 发布:2024-09-20 16:03:20 浏览:744
mac执行python脚本 发布:2024-09-20 15:58:52 浏览:778
单片机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