欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java动态显示当前日期和时间

时间:2021-08-20 10:03:41|栏目:JAVA代码|点击:

Java 动态显示当前系统的日期、时间;如图所示:

package com.xin.test;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.JFrame;
 
public class NowTime extends JFrame {
 
 
 private static final long serialVersionUID = 4306803332677233920L;
 
 // 添加 显示时间的JLabel
 public NowTime() {
 JLabel time = new JLabel();
 time.setForeground(Color.BLUE);
 time.setBounds(30, 0, 900, 130);
 time.setFont(new Font("微软雅黑", Font.BOLD, 80));
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setLayout(null);
 this.setTitle("now time");
 this.setBounds(500, 200, 930, 200);
 this.setVisible(true);
 this.add(time);
 this.setTimer(time);
 }
 
 // 设置Timer 1000ms实现一次动作 实际是一个线程
 private void setTimer(JLabel time) {
 final JLabel varTime = time;
 Timer timeAction = new Timer(100, 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) {
 new NowTime();
 }
 
}

上一篇:解析spring-security权限控制和校验的问题

栏    目:JAVA代码

下一篇:解决TreeSet类的排序问题

本文标题:Java动态显示当前日期和时间

本文地址:http://www.codeinn.net/misctech/168282.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有