java中获取动态时间间隔_java获取动态时间

java获取动态时间

关注:160  答案:4  mip版

解决时间 2021-01-27 03:03

e6cb1a03ad541b3098697807b7bf1798.png

提问者習慣邇的習慣

2021-01-26 20:16

在用户界面上显示的时间能够自己动

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家等待、埖开

2021-01-26 20:31

建议使用线程来做。

使用Thread.sleep(1000);即可停止一秒。

class TimeThread implements Runnable{

private JLabel label;

public TimeThread(JLabel l){

label=l;

}

public void run(){

while(true){

Thread.sleep(1000);

setTime();

}

}

private void setTime(){

//在这里设置label的值即可

}

}

调用的时候,你要在时钟的主界面生成后,使用

new Thread(new TimeThread(label)).start();即可

其中label为你显示时间的JLabel

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼一个人,一辈子

2021-01-26 22:15

import java.awt.Dimension;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class TimeFrame extends JFrame

{

private JPanel timePanel;

private JLabel timeLabel;

private JLabel displayArea;

private String DEFAULT_TIME_FORMAT = "HH:mm:ss";

private String time;

private int ONE_SECOND = 1000;

public TimeFrame()

{

timePanel = new JPanel();

timeLabel = new JLabel("CurrentTime: ");

displayArea = new JLabel();

configTimeArea();

timePanel.add(timeLabel);

timePanel.add(displayArea);

this.add(timePanel);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(new Dimension(200,70));

this.setLocationRelativeTo(null);

}

private void configTimeArea() {

Timer tmr = new Timer();

tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);

}

protected class JLabelTimerTask extends TimerTask{

SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

@Override

public void run() {

time = dateFormatter.format(Calendar.getInstance().getTime());

displayArea.setText(time);

}

}

public static void main(String arg[])

{

TimeFrame timeFrame=new TimeFrame();

timeFrame.setVisible(true);

}

}

e6cb1a03ad541b3098697807b7bf1798.png

2楼喜欢就甘愿

2021-01-26 21:13

你装的是xp系统吧 这是xp的一个补丁造成的 改补丁修改了注册表中对时区的设置致使java获取时间错误 你把周围人的注册表(hkey_local_machine\system\currentcontrolset\control\timezoneinformation) copy一份到你的电脑上,你的注册表有问题。 或者把一下代码复制到记事本上,后缀改为:1.reg,然后双击,点是。 如果还不行请联系我。 ------------- windows registry editor version 5.00 [hkey_local_machine\system\currentcontrolset\control\timezoneinformation] "bias"=dword:fffffe20 "standardname"="中国标准时间" "standardbias"=dword:00000000 "standardstart"=hex:00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 "daylightname"="中国标准时间" "daylightbias"=dword:00000000 "daylightstart"=hex:00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 "activetimebias"=dword:fffffe20

e6cb1a03ad541b3098697807b7bf1798.png

3楼心苍凉

2021-01-26 21:04

Date date = new Date(System.currentTimeMillis());

for ( int i = 0; i < 100; i++ ) {

Thread.sleep(500);

date.setTime(System.currentTimeMillis());

System.out.println("Current time is: " + date.toString());

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值