java程序代码实例计时 java 计时函数

Java怎么给方法计时你可以在开始和结束的时候,分别记录下当前的时间的这毫秒数 。然后再减,以下是一段代码 。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
System.out.println("开始 "+startMili);
// 执行一段代码,求一百万次随机值
for(int i=0;i1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("结束 s"+endMili);
System.out.println("总耗时为:"+(endMili-startMili)+"毫秒");
}
}
JAVA中如何使用计时函数Java计时函数currentTimeMills()
System.currentTimeMills()计时精确到毫秒级java程序代码实例计时 , 跟计算机以1970年1月1日0时为计时起点一样java程序代码实例计时,该函数方法统计java程序代码实例计时的也是从1970年1月1日0时开始 , 到程序运行到该函数时刻java程序代码实例计时的毫秒总数 。
该函数方法定义在Java系统类System中 , 如果想实现程序运行计时功能也很简单,只要在程序前后分别放置该函数方法 , 然后后减前毫秒总数,就能计算程序运行的耗时 。具体实现如下java程序代码实例计时:
long startTime = System.currentTimeMills(); //程序开始记录时间
// 。。。。。。
long endTime= System.currentTimeMills(); //程序结束记录时间
long TotalTime = endTime - startTime;//总消耗时间
如何用java实现一个计时器?用java实现一个计时器的方法:
public class TestDingShi implements Runnable
{
Thread xc;
Dao dao=new DaoImpl();
public TestDingShi()
{
xc=new Thread(this);//线程开启
xc.start();
}
public void run()
{
while (true)
{
try
{
xc.sleep(1000);//睡眠开始计时
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO定时在此
}
}
}
java 中网页上计时功能怎么实现?!DOCTYPE html
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
titleRunJS/title
style
#t {
margin: 0 auto;
border: 1px solid red;
width: 200px;
line-height: 60px;
height: 60px;
text-align: center;
}
#p {
font: bolder 22px 宋体;
color: red;
}
/style
script
var h = new Date("1/1/1,0:1:0");
var it = null;
onload = function() {
it = setInterval(function() {
var hh = h.getHours();
var m = h.getMinutes();
var s = h.getSeconds();
if (hh == 0m == 0s == 0) {
clearInterval(it);
alert("时间到");
}
hh = hh10 ? "0" + hh : hh;
m = m10 ? "0" + m : m;
s = s10 ? "0" + s : s;
p.innerHTML = hh + ":" + m + ":" + s;
h.setSeconds(s - 1);
}, 1000);
}
/script
/head
body
div id="t"
span id="p"/span
/div
/body
/html
用java编写一个倒计时器代码 。import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener {private static final long serialVersionUID = 201306211111L;private JTextField screen = new JTextField("0");private JButton start = new JButton("开始");private JButton reset = new JButton("重置");private JPanel panel = new JPanel();private boolean isRunning;private int time;private int timeBetween;public TimerDemo(int timeBetween) {super("计时器");this.timeBetween = timeBetween;try {init();} catch (Exception e) {e.printStackTrace();}}public TimerDemo() {super("计时器");this.timeBetween = 100;try {init();} catch (Exception e) {e.printStackTrace();}}private void init() {panel.setLayout(new GridLayout());panel.add(start);panel.add(reset);start.addActionListener(this);reset.addActionListener(this);screen.setFont(new Font("幼圆", Font.BOLD, 60));screen.setHorizontalAlignment(JTextField.CENTER);screen.setEditable(false);Container c = getContentPane();c.setLayout(new BorderLayout());c.add(panel, BorderLayout.SOUTH);c.add(screen, BorderLayout.CENTER);this.setSize(200, 150);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setLocationRelativeTo(null);this.setVisible(true);}public static void main(String[] args) {new TimerDemo(1);// 设定 1ms/次// new TimerDemo();}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == start) {if (start.getText().equals("开始")) {start.setText("暂停");isRunning = true;} else if (start.getText().equals("暂停")) {start.setText("开始");isRunning = false;}}if (e.getSource() == reset) {start.setText("开始");screen.setText("0");isRunning = false;time = 0;}new Thread(new TimeZone()).start();}class TimeZone implements Runnable {@Overridepublic void run() {while (isRunning) {time++;if (time = Integer.MAX_VALUE) {screen.setText("ERROR");JOptionPane.showMessageDialog(null, "ERROR");isRunning = false;}screen.setText(String.valueOf(time));try {Thread.sleep(timeBetween);} catch (Exception e) {e.printStackTrace();}}}}}

推荐阅读