这个java窗体怎么显示动态时间?
你是想实时更新状态栏上的时间?
可以用定时器自动去更新
import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Clock extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Clock().setVisible(true);
}
});
}
private JLabel timeLabel;
public Clock() {
timeLabel = new JLabel(String.valueOf(new Date()), JLabel.CENTER);
this.getContentPane().add(timeLabel, BorderLayout.CENTER);
this.setBounds(0, 0, 300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Timer(“clock”).schedule(new TimerTask() {
@Override
public void run() {
timeLabel.setText(String.valueOf(new Date()));
}
}, 0, 1000);
}
}
在java中,要如何在一个文本框动态显示时间?
html
head
title时钟特效/title
script type=”text/javascript”
function disptime(){
var today = new Date(); //获得当前时间
var hh = today.getHours(); //获得小时、分钟、秒
var mm = today.getMinutes();
var ss = today.getSeconds();
/*设置div的内容为当前时间*/
document.getElementById(“myclock”).innerHTML=”h1现在是:”+hh+”:”+mm+”:”+ss+”h1″;
document.getElementById(“myClock1″).value=hh+”:”+mm+”:”+ss;
/*
使用setTimeout在函数disptime()体内再次调用setTimeout
设置定时器每隔1秒(1000毫秒),调用函数disptime()执行,刷新时钟显示
*/
var myTime=setTimeout(“disptime()”,1000);
}
/script
/head
body onload=”disptime()”
div id=”myclock”/div
input type=”text” id=”myClock1″ value=””/input
/body
/html
java动态时间代码
你应该是想把时间显示在jl01的控件上。但是你这样只能显示初始化时的时间。
每隔1s更新时间,简单一点可以用循环,然后每次循环设置1000ms的睡眠时间,这个同时需要用多线程实现。也可以用timer做定时任务。推荐后者
//import java.util.Timer
//import java.util.TimerTask
Timer timer = new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
Date date = new Date();
String string = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(date);
jl01.setText(string);
}
};
//立即开始任务,任务间隔1000ms。schedule和scheduleAtFixedRate的区别自行搜索
timer.scheduleAtFixedRate(task,0,1000);
java里如何实现动态时间
import java.awt.Font;
import java.util.Date;
import javax.swing.*;
public class ListTime extends JFrame implements Runnable {
JLabel lbl = null;
ListTime() {
setLayout(null);
setTitle(“时间显示”);
setBounds(200, 200, 400, 300);
lbl = new JLabel(“”);
lbl.setBounds(20, 20, 300, 200);
lbl.setFont(new Font(“Arial”, Font.BOLD, 20));
add(lbl);
setVisible(true);
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
lbl.setText(new Date().toString());
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
ListTime f = new ListTime();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thread myThread = new Thread(f);
myThread.start();
}
}
java如何获取当前时间 年月日 时分秒
//得到long类型当前时间
long l = System.currentTimeMillis();
//new日期对
Date date = new Date(l);
//转换提日期输出格式
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-
dd HH:mm:ss”);System.out.println(dateFormat.format(date));
扩展资料
package com.ob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
Calendar now = Calendar.getInstance();
System.out.println(“年: ” + now.get(Calendar.YEAR));
System.out.println(“月: ” + (now.get(Calendar.MONTH) + 1) + “”);
System.out.println(“日: ” + now.get(Calendar.DAY_OF_MONTH));
System.out.println(“时: ” + now.get(Calendar.HOUR_OF_DAY));
System.out.println(“分: ” + now.get(Calendar.MINUTE));
System.out.println(“秒: ” + now.get(Calendar.SECOND));
System.out.println(“当前时间毫秒数:” + now.getTimeInMillis());
System.out.println(now.getTime());
\t\tDate d = new Date();
System.out.println(d);
\t\tSimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
\t\tString dateNowStr = sdf.format(d);
System.out.println(“格式化后的日期:” + dateNowStr);
\t\t
\t\tString str = “2012-1-13 17:26:33”;
//要跟上面sdf定义的格式一样
\t\tDate today = sdf.parse(str);
System.out.println(“字符串转成日期:” + today);
\t}
}
参考资料:Java – 百度百科
Java中如何实现显示动态的时间?
利用死循环和线程,让线程在循环中每sleep1秒,重新获取下系统时间不就是动态显示时间了吗
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(date);
//每一秒刷新下时间
try {
Thread.sleep(1000);//sleep是以ms为单位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你可以试下代码,看看是不是你要的效果