为了账号安全,请及时绑定邮箱和手机立即绑定

程序错误""显示系统当前年月日时分秒 ""该怎么办?

程序错误""显示系统当前年月日时分秒 ""该怎么办?

慕标琳琳 2022-10-13 11:11:35
import java.awt.*;import java.awt.event.*;import java.util.Calendar;import javax.swing.*;public class timeTest extends JFrame{final timeTest ti=this;private JLabel label=new JLabel(); public timeTest(){this.setSize(800,400);this.setTitle("时间");this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Toolkit tk=this.getToolkit();Dimension screenSize=tk.getScreenSize();this.setLocation(screenSize.width/4-this.getWidth()/4, screenSize.height/4-this.getHeight()/4);label.setBounds(200,80,480,80); this.getContentPane().add(label); ActionListener taskPerformer=new ActionListener(){public void actionPerformed(ActionEvent e){Calendar calendar=new Calendar();label.setText(calendar.get(Calendar.YEAR) - 1900 +"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_WEEK)+"日"+ calendar.get(Calendar.HOUR_OF_DAY) +":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND));label.setFont(new Font("黑体",Font.PLAIN,25));};new Timer(1000, taskPerformer).start(); this.setVisible(true); }public static void main(String[] args) {new timeTest();}}
查看完整描述

3 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

Date nowDate = nowTime.getTime();
public static String getTime(long time) {
Date da = new Date(time);
String dateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(da);
}

查看完整回答
反对 回复 2022-10-16
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

calendar.get(Calendar.YEAR) - 1900 +"年"+
calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DAY_OF_WEEK)
+"日"+ calendar.get(Calendar.HOUR_OF_DAY) +":"+calendar.get(Calendar.MINUTE)
+":"+calendar.get(Calendar.SECOND)) 改成:
SimpleDateFormat sf=new SimpleDateFormat("yyyy年-MM月-dd日 hh时:mm分:ss秒");
String s=sf.format(new Date());

查看完整回答
反对 回复 2022-10-16
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

Calendar 不是用来New 的!通过Calendar.getInstance();来创建的!

查看完整回答
反对 回复 2022-10-16
  • 3 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信