显示时间java
❶ java窗口显示当前时间
用 java的 swing做个图形界面 然后显示当前的时间:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.JFrame;
public class NowTime extends JFrame
{
//添加 显示时间的JTextField
private void addComponent(){
JTextField time = new JTextField();
this.getContentPane().add(time);
this.setTimer(time);
}
//显示窗口
private void showTime(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pack();//自动适应窗口大小
this.setSize(160, 80);
this.setVisible(true);
}
//设置Timer 1000ms实现一次动作 实际是一个线程
private void setTimer(JTextField time){
final JTextField varTime = time;
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
//转换日期显示格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
varTime.setText(df.format(new Date(timemillis)));
}
});
timeAction.start();
}
//运行方法
public static void main(String[] args) {
NowTime timeFrame = new NowTime();
timeFrame.addComponent();
timeFrame.showTime();
}
}
❷ 在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程序如何实现时间的显示
你可以利用这个
timer.scheleAtFixedRate(new sysTimeRefresher(), 0, 1000);<---每秒读一次(可以自己改)
class SysTimeRefresher extends TimerTask {
@Override
synchronized public void run() {
Calendar.getInstance().getTime()<----这个是抓取现在系统时间隔式请自己看要哪一种
这里可以实做显示时间
timer.cancel();<----这是实作16:30时间到中只运行
}}
❹ java如何在一个文本框动态显示时间
packagecn.test;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Timer;
importjavax.swing.JFrame;
importjavax.swing.JTextField;
publicclassTimeextendsJFrame{
privateJTextFieldTimeBox=newJTextField();
privateTimertimer=newTimer();
privateSimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss");
publicTime(){
TimeBox.setBounds(0,0,100,50);
add(TimeBox);
//方法1定时器方法
//TimerTasktask=newTimerTask(){
//@Override
//publicvoidrun(){
//TimeBox.setText(sdf.format(newDate()));
//}
//};
//timer.schele(task,1000,1000);
//方法2线程休眠方法
Displaytimedisplay=newDisplay();
timedisplay.start();
}{
SimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss");
publicDisplay(){
System.out.println("display");
}
@Override
publicvoidrun(){
while(true){
System.out.println("Threadisrunning...");
TimeBox.setText(sdf.format(newDate()));
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
System.out.println("sleeperror!!");
e.printStackTrace();
}
}
}
}
publicstaticvoidmain(String[]args){
Timet=newTime();
t.setSize(300,200);
t.setResizable(false);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
}
❺ java 动态显示时间
javax.swing.Timer ,很容易的,一个事件,在事件里面显示你的时间就可以了。
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class TimerShow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TimerShow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private JLabel lblNewLabel;
public TimerShow() {
super("time");
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
lblNewLabel = new JLabel(" 简单时钟 ", JLabel.CENTER);
lblNewLabel.setBounds(0, (300-35-35)/2, 440, 35);
getContentPane().add(lblNewLabel);
setVisible(true);
new Timer(delay, taskPerformer).start();
}
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String s=String.format("%tY-%<tm-%<td %<tH:%<tM:%<tS",new Date());
lblNewLabel.setText(s);
}
};
}
❻ Java显示当前日期时间
介绍2个对象你用用吧。
public static void main(String[] args) {
//日期格式转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = sdf.format(new Date());
System.out.println(datetime);
//Calendar 有N多关于日期的操作。你可以看看帮助文档。
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
}
❼ Java 如何显示当前系统日期与时间
通过new Date获取当前的日期与时间
示例:
publicstaticvoidmain(String[]args){
Datenow=newDate();//获取当前时间
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss");//格式化当前日期时间,显示如2015/06/2714:22:22
}
❽ Java时间显示
请参考
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.SimpleDateFormat;
class MyJDialog extends JDialog{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public MyJDialog(){
Date date=new Date();
String now=fmt.format(date);
setTitle("当前时间");
Container c=getContentPane();
JLabel jl=new JLabel("点前的系统时间是:"+now);
jl.setHorizontalAlignment(SwingConstants.CENTER);
c.add(jl);
setBounds(140,140,400,200);
}
}
public class JDialogDemo extends JFrame{
public JDialogDemo(){
setTitle("系统时间显示软件");
Container c=getContentPane();
setLayout(null);
JButton jb=new JButton("系统时间");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new MyJDialog().setVisible(true);
}
});
jb.setBounds(50,50,180,50);
c.add(jb);
setBounds(100,100,300,200);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JDialogDemo();
}
}
❾ 一段显示当下时间的JAVA代码
privateShaperect;//背景矩形
privateFontfont;//设置字体
privateDatedate;//现在的时间
privateThreadtime;//时间线程
privateCanvasPanelcanvas;
publicstaticvoidmain(String[]args){
newTimerTest20140930();
}
publicTimerTest20140930(){
super("绘制文本");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
rect=newRectangle2D.Double(10,10,200,100);
font=newFont("宋体",Font.BOLD,16);
canvas=newCanvasPanel();
add(canvas);
time=newThread(newRunnable(){
publicvoidrun(){
while(true){
canvas.repaint();
try{
Thread.sleep(1000);
}catch(Exceptionex){
}
}
}
});
time.start();
setLocationRelativeTo(null);
setVisible(true);
}
classCanvasPanelextendsCanvas{
publicvoidpaint(Graphicsg){
super.paint(g);
Graphics2Dg2=(Graphics2D)g;
g2.setColor(Color.BLACK);
g2.fill(rect);
g2.setColor(Color.BLUE);
g2.setFont(font);
g2.drawString("现在的时间是",20,30);
date=newDate();
g2.drawString(String.format("%tr",date),50,60);
}
}
❿ java 编程中显示日期和时间的代码
可以直接通过jdk基本方法,获取到当前的时间
Date date= new Date();//创建一个时间对象,获取到当前的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间显示格式
String str = sdf.format(date);//将当前时间格式化为需要的类型
System.out.println(str);//输出结果
结果为:2015-11-06 13:53:54(实时)。