将日期加载到JTextField中 [英] Loading date into JTextField

查看:105
本文介绍了将日期加载到JTextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将日期加载到JTextField时遇到一些问题.我犯了什么错误?

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

推荐答案

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

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

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

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

  • 使用Calendar()获取当前日期时间

  • get current date time with Calendar()

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

  • 这篇关于将日期加载到JTextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆