网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java时间计时器代码,java时钟代码

求java的计时器代码,应该比较简单的,来看看吧。

package test;

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站建设、象山网络推广、小程序开发、象山网络营销、象山企业策划、象山品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供象山建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

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("复位");

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("继续")) {

swThread.go();

btnPouse.setLabel("暂停");

} else if (btnPouse.getLabel().equals("暂停")) {

swThread.noGo();

btnPouse.setLabel("继续");

}

}

}

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语句里面的值来判断什么时候停止

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 秒表

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

// 计数线程

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("继续");

}

}

};

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(); // 计数线程一直就运行着

}

// 为窗体面板添加边框

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

}

}

}

// 将毫秒数格式化

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

}

}

}

你可以试试,希望能帮到你!

JAVA计时器,怎么写

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

}

}

}

满意请采纳。

java定时器

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秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.

while (true) {// 这个是用来停止此任务的,否则就一直循环执行此任务了

try {

int ch = System.in.read();

if (ch - 'c' == 0) {

timer.cancel();// 使用这个方法退出任务

}

} 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 当然如过太复杂了而且这种定时需求很多的话 建议用quartz框架 使用很简单

用JAVA编写计时器

计时器可以使用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();

}

}

}

}


新闻名称:java时间计时器代码,java时钟代码
标题来源:http://bjjierui.cn/article/dsesicp.html

其他资讯