我有这个代码:
class ClockLabel extends JLabel implements ActionListener {
public ClockLabel() {
super("" + new java.util.Date());
Timer t = new Timer(1000, this);
t.start();
}
@Override
public void actionPerformed(ActionEvent ae) {
txtclock.setText((new java.util.Date()).toString());
}
}
我的输出是
Wed Feb 06 14:22:44 CST 2013
如何以这种格式更改输出
"MM dd yyyy HH:mm:ss"
答案 0 :(得分:3)
您可以使用SimpleDateFormat格式化日期
SimpleDateFormat f = new SimpleDateFormat("MM dd yyyy HH:mm:ss");
txtclock.setText(f.format(new java.util.Date()));
答案 1 :(得分:2)
查看SimpleDateFormat
课程。你可以构造SimpleDateformat with patteryn
“MM dd yyyy HH:mm:ss”and pass the date object in
format()`的实例。
答案 2 :(得分:2)
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy HH:mm:ss");
String sDate= sdf.format(new java.util.Date());
答案 3 :(得分:2)
答案 4 :(得分:1)
@Override
public void actionPerformed(ActionEvent ae) {
SimpleDateFormat format = new SimpleDateFormat("MM dd yyyy HH:mm:ss");
txtclock.setText(format.format(new java.util.Date()));
}
答案 5 :(得分:1)
将代码修改为
class ClockLabel extends JLabel implements ActionListener {
java.util.Date date;
public ClockLabel() {
super(getStrVAl());
Timer t = new Timer(1000, this);
t.start();
}
@Override
public void actionPerformed(ActionEvent ae) {
txtclock.setText(getStrVAl());
}
public String getStrVAl() {
date=new java.util.Date();
String val=date.getMonth().toString()+" "+date.getDate().toString()+" "+date.getYear().toString()+" "date.getHours().toString()+":"+date.getMinutes().toString()+":"+date.getSeconds()().toString()+" ");
return val;
}
}