Loading date into JTextField [closed]

前提是你 提交于 2019-12-13 08:14:11

问题


I had some problems loading a date into a JTextField. What mistakes did I make?

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);
  }
}
//--------------------------------------------------------------------------- 

回答1:


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

  1. 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));
    
  2. get current date time with Calendar()

    //get current date time with Calendar()
       Calendar cal = Calendar.getInstance();
       jTextField3.setText(dateFormat.format(cal.getTime()));
    


来源:https://stackoverflow.com/questions/16285019/loading-date-into-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!