package?test;

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站建設(shè)、象山網(wǎng)絡(luò)推廣、小程序開發(fā)、象山網(wǎng)絡(luò)營銷、象山企業(yè)策劃、象山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供象山建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:chinadenli.net
import?java.util.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.applet.*;
public?class?Test5?extends?Applet?{
private?final?Panel?pan?=?new?Panel();
private?final?Label?time?=?new?Label();
private?final?Button?btnGo?=?new?Button("開始");
private?final?Button?btnPouse?=?new?Button("暫停");
private?final?Button?btnReset?=?new?Button("復(fù)位");
private?final?StopwatchThread?swThread?=?new?StopwatchThread();
private?class?btnGoListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???
swThread.go();
btnGo.setEnabled(false);
}
}
private?class?btnPouseListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
???if(btnGo.isEnabled()){
???return?;
???}
?if?(btnPouse.getLabel().equals("繼續(xù)"))?{
swThread.go();
btnPouse.setLabel("暫停");
}?else?if?(btnPouse.getLabel().equals("暫停"))?{
swThread.noGo();
btnPouse.setLabel("繼續(xù)");
}
}
}
private?class?btnResetListener?implements?ActionListener?{
public?void?actionPerformed(ActionEvent?e)?{
swThread.reset();
btnGo.setEnabled(true);
btnGo.setLabel("開始");
btnPouse.setLabel("暫停");
}
}
private?class?StopwatchThread?extends?Thread?{
private?boolean?going?=?false;
private?long?prevElapsed?=?0;
private?Date?startDate?=?new?Date();
private?long?elapsedTime()?{
return?prevElapsed?+
(going???new?Date().getTime()?-?startDate.getTime()?:?0);
}
private?String?msToString(long?time)?{
???System.out.println(time+"??"+((0*60+2)*1000+999));
if(((99*60+59)*1000+983)=time((99*60+59)*1000+999)=time){//((0*60+2)*1000+983)=time((0*60+2)*1000+999)=time
if?(time?%?1000??990)
time?+=?2;
swThread.noGo();
}
String?ms,?sec,?min;
if?(time?%?10?=?5)
time?+=?5;
ms?=?Long.toString(time?%?1000);
while?(ms.length()??3)
ms?=?"0"?+?ms;
ms?=?ms.substring(0,?ms.length()?-?1);
time?/=?1000;
sec?=?Long.toString(time?%?60);
if?(sec.length()?==?1)?sec?=?"0"?+?sec;
time?/=?60;
min?=?Long.toString(time);
return?min?+?":"?+?sec?+?"."?+?ms;
}
public?void?go()?{
startDate?=?new?Date();
going?=?true;
}
public?void?noGo()?{
prevElapsed?=?elapsedTime();
going?=?false;
}
public?void?reset()?{
going?=?false;
prevElapsed?=?0;
}
public?void?run()?{
while?(true)?{
time.setText(msToString(elapsedTime()));
yield();
}
}
}
public?void?init()?{
setLayout(new?GridLayout(2,2));
setBackground(Color.lightGray);
setForeground(Color.black);
pan.setLayout(new?GridLayout(3,2));
pan.add(new?Label("計時:"));
time.setForeground(Color.blue);
pan.add(time);
pan.add(btnGo);
pan.add(btnPouse);
pan.add(btnReset);
pan.add(new?Label());
add(pan);
btnGo.addActionListener(new?btnGoListener());
btnReset.addActionListener(new?btnResetListener());
btnPouse.addActionListener(new?btnPouseListener());
swThread.setDaemon(true);
swThread.start();
}
public?static?void?main(String[]?args)?{
Test5?applet?=?new?Test5();
Frame?aFrame?=?new?Frame("計時器");
aFrame.addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
aFrame.add(applet,?BorderLayout.CENTER);
aFrame.setSize(400,?200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
可以改變有注釋的那個if語句里面的值來判斷什么時候停止
你可以在開始和結(jié)束的時候,分別記錄下當(dāng)前的時間的這毫秒數(shù)。然后再減,以下是一段代碼。
public class Test{
public static void main(String[] args) {
long startMili=System.currentTimeMillis();// 當(dāng)前時間對應(yīng)的毫秒數(shù)
System.out.println("開始 "+startMili);
// 執(zhí)行一段代碼,求一百萬次隨機值
for(int i=0;i1000000;i++){
Math.random();
}
long endMili=System.currentTimeMillis();
System.out.println("結(jié)束 s"+endMili);
System.out.println("總耗時為:"+(endMili-startMili)+"毫秒");
}
}
package demo;
import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Timer extends JFrame {
private static final long serialVersionUID = 1L;
private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
// 計數(shù)線程
private CountingThread thread = new CountingThread();
// 記錄程序開始時間
private long programStart = System.currentTimeMillis();
// 程序一開始就是暫停的
private long pauseStart = programStart;
// 程序暫停的總時間
private long pauseCount = 0;
private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
private JButton startPauseButton = new JButton("開始");
private JButton resetButton = new JButton("清零");
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
thread.stopped = false;
startPauseButton.setText("暫停");
} else {
pauseStart = System.currentTimeMillis();
thread.stopped = true;
startPauseButton.setText("繼續(xù)");
}
}
};
private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart;
pauseCount = 0;
thread.stopped = true;
label.setText(INITIAL_LABEL_TEXT);
startPauseButton.setText("開始");
}
};
public Timer(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300, 300);
setResizable(false);
setupBorder();
setupLabel();
setupButtonsPanel();
startPauseButton.addActionListener(startPauseButtonListener);
resetButton.addActionListener(resetButtonListener);
thread.start(); // 計數(shù)線程一直就運行著
}
// 為窗體面板添加邊框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
}
// 配置按鈕
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(startPauseButton);
panel.add(resetButton);
add(panel, BorderLayout.SOUTH);
}
// 配置標簽
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
this.add(label, BorderLayout.CENTER);
}
// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Timer frame = new Timer("計時器");
frame.pack();
frame.setVisible(true);
}
private class CountingThread extends Thread {
public boolean stopped = true;
private CountingThread() {
setDaemon(true);
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount;
label.setText(format(elapsed));
}
try {
sleep(1); // 1毫秒更新一次顯示
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
// 將毫秒數(shù)格式化
private String format(long elapsed) {
int hour, minute, second, milli;
milli = (int) (elapsed % 1000);
elapsed = elapsed / 1000;
second = (int) (elapsed % 60);
elapsed = elapsed / 60;
minute = (int) (elapsed % 60);
elapsed = elapsed / 60;
hour = (int) (elapsed % 60);
return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);
}
}
}
你可以試試,希望能幫到你!
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TimeCount extends JFrame implements ActionListener{
ThreadCount tc=new ThreadCount(this);
Thread thread=new Thread(tc);
JPanel panelN=new JPanel(),panelC=new JPanel();
JLabel label=new JLabel("計時器");
JButton butnStart=new JButton("開始");
boolean toEnd;
public TimeCount() {
setBounds(100,100,300,300);
setVisible(true);
label.setFont(new Font(null,Font.BOLD,22));
panelN.add(label);
add(panelN,BorderLayout.NORTH);
panelC.add(butnStart);
add(panelC,BorderLayout.CENTER);
butnStart.addActionListener(this);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==butnStart){
if(!thread.isAlive()){
thread=new Thread(tc);
thread.start();
}else {
toEnd=true;
}
}
}
public static void main(String[] args) {
new TimeCount();
}
}
class ThreadCount implements Runnable{
TimeCount lc;
public ThreadCount(TimeCount lc) {
super();
this.lc = lc;
}
public void run() {
int i=1;
while(true){
if(lc.toEnd){
lc.toEnd=false;
lc.butnStart.setText("開始");
return;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO: handle exception
}
i+=2;
int min=i/60000;
int second=(i%60000)/1000;
int mm=i%1000;
String show="";
if(min0)
show+=min+":";
if(second0)
show+=second+".";
show+=mm;
lc.label.setText(show);
}
}
}
滿意請采納。
import?java.io.IOException;
import?java.util.Timer;
public?class?TimerTest?{
public?static?void?main(String[]?args)?{
Timer?timer?=?new?Timer();
timer.schedule(new?MyTask(),?1000,?2000);//?在1秒后執(zhí)行此任務(wù),每次間隔2秒,如果傳遞一個Data參數(shù),就可以在某個固定的時間執(zhí)行這個任務(wù).
while?(true)?{//?這個是用來停止此任務(wù)的,否則就一直循環(huán)執(zhí)行此任務(wù)了
try?{
int?ch?=?System.in.read();
if?(ch?-?'c'?==?0)?{
timer.cancel();//?使用這個方法退出任務(wù)
}
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
static?class?MyTask?extends?java.util.TimerTask?{
@Override
public?void?run()?{
//?TODO?Auto-generated?method?stub
System.out.println("________");
}
}
}
這段代碼基本能滿足你需求了 你還有需求就在上面再套一層job 當(dāng)然如過太復(fù)雜了而且這種定時需求很多的話 建議用quartz框架 使用很簡單
計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
public?class?Calculagraph?extends?Thread?{
public?static?void?main(String[]?args)?{
new?Calculagraph().start();
}
private?long?now?=?0l;
private?long?start?=?System.currentTimeMillis();//?程序啟動時間的毫秒值
private?long?time;
public?void?run()?{
while?(true)?{
now?=?System.currentTimeMillis();//?獲取一秒之后的毫秒值
time?=?now?-?start;//?兩個時間相減的到毫秒差
System.out.format("%02d:%02d:%02d\n",
time?/?(1000?*?60?*?60)?%?60/*?時?*/,?
time?/?(1000?*?60)%?60/*?分?*/,?
time?/?1000?%?60/*?秒?*/);//?格式化字符串輸出
try?{
Thread.sleep(1000);
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
}
}
當(dāng)前題目:java時間計時器代碼,java時鐘代碼
標題網(wǎng)址:http://chinadenli.net/article28/dsesicp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、企業(yè)網(wǎng)站制作、定制網(wǎng)站、定制開發(fā)、建站公司、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)