温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-10-15 17:54:23 来源:亿速云 阅读:157 作者:柒染 栏目:编程语言

这期内容当中小编将会给大家带来有关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(); } }

上述就是小编为大家分享的Java动态如何显示当前日期和时间了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI