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编程时 如何进行计时?在时间内完成则显示时间,没有完成则跳出程序给你代码 。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private JTextField text = null;
private JTextArea area = null;
private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
private Calendar time = Calendar.getInstance();
private JButton start = null;
private JButton complete = null;
private Timer timer = null;
public Test() {
time.set(Calendar.MINUTE, 25);
time.set(Calendar.SECOND, 0);
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
JLabel label = new JLabel("time:");
text = new JTextField("25:00");
text.setEditable(false);
top.add(label, BorderLayout.WEST);
top.add(text, BorderLayout.CENTER);
add(top, BorderLayout.NORTH);
area = new JTextArea();
area.setEnabled(false);
JScrollPane scrollpane = new JScrollPane(area);
add(scrollpane, BorderLayout.CENTER);
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
start = new JButton("start");
start.addActionListener(this);
buttons.add(start);
complete = new JButton("Complete");
complete.addActionListener(this);
buttons.add(complete);
【java计时代码完成时间 java程序计时】add(buttons, BorderLayout.SOUTH);
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if ("start".equals(e.getActionCommand())) {
area.setEnabled(true);
timer = new Timer();
timer.schedule(new TimeTask(), new Date(), 1000);
start.setEnabled(false);
} else {
start.setEnabled(true);
area.setEnabled(false);
timer.cancel();
}
}
class TimeTask extends TimerTask {
@Override
public void run() {
time.add(Calendar.SECOND, -1);
String strTime = sdf.format(time.getTime());
text.setText(strTime);
if ("00:00".equals(strTime)) {
start.setEnabled(true);
area.setEnabled(false);
this.cancel();
}
}
}
}
求人用java编写一条计时器代码 。import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;

推荐阅读