0

我在将日期加载到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);
  }
}
//--------------------------------------------------------------------------- 
4

1 回答 1

3

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

  1. 使用 Date() 获取当前日期时间

    //get current date time with Date()
       Date date = new Date();
       System.out.println(dateFormat.format(date));
       jTextField3.setText(dateFormat.format(date));
    
  2. 使用 Calendar() 获取当前日期时间

    //get current date time with Calendar()
       Calendar cal = Calendar.getInstance();
       jTextField3.setText(dateFormat.format(cal.getTime()));
    
于 2013-04-29T18:07:11.640 回答