java 将日期加载到 JTextField
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16285019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:30:48 来源:igfitidea点击:
Loading date into JTextField
提问by Vilius
I had some problems loading a date into a JTextField
. What mistakes did I make?
我在将日期加载到JTextField
. 我犯了什么错误?
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewApplication().setVisible(true);
}
});
}
//---------------------------------------------------------------------------
public class FormatedDate {
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
String reportDate = ft.format(dNow);
public void LoadDate() {
jTextField3.setText(reportDate);
System.out.println("Current Date: " + reportDate);
}
}
//---------------------------------------------------------------------------
回答by Milan
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
get current date time with Date()
//get current date time with Date() Date date = new Date(); System.out.println(dateFormat.format(date)); jTextField3.setText(dateFormat.format(date));
get current date time with Calendar()
//get current date time with Calendar() Calendar cal = Calendar.getInstance(); jTextField3.setText(dateFormat.format(cal.getTime()));
使用 Date() 获取当前日期时间
//get current date time with Date() Date date = new Date(); System.out.println(dateFormat.format(date)); jTextField3.setText(dateFormat.format(date));
使用 Calendar() 获取当前日期时间
//get current date time with Calendar() Calendar cal = Calendar.getInstance(); jTextField3.setText(dateFormat.format(cal.getTime()));