Link to home
Create AccountLog in
Avatar of osama120
osama120

asked on

set the JTextField with curent date

im using a class that get the curent
import java.util.Date;
import java.awt.event.*;
import javax.swing.*;
public class CurrentTime extends JTextField implements ActionListener {

  public CurrentTime() {
   super(""+ new Date());
   
  }

  public void actionPerformed(ActionEvent ae) {
    setText((new Date()).toString());
 }
}
the problem is the class show the JTextField show the in this format Sun Mar 09 13:51:20 AST 2008
and i want to be in this format
9-Mar-2008 13:20:51
im using netbeans ide
Avatar of arioh
arioh
Flag of Russian Federation image

     java.text.SimpleDateFormat DF = new java.text.SimpleDateFormat( "dd-MMM-yyyy HH:mm:ss" );
      setText( DF.format( new Date() );
Avatar of osama120
osama120

ASKER

no still give the same result i think the change should from the constrauctor
public CurrentTime() {
   super(""+ new Date());
   
  }
public class CurrentTime extends JTextField implements ActionListener {

  public CurrentTime() {
   super( dateToString( new Date() ) );
  }

  public void actionPerformed(ActionEvent ae) {
    setText( dateToString( new Date() ) );
 }

 private String dateToString( Date DT ) {
  try {
    java.text.SimpleDateFormat DF = new java.text.SimpleDateFormat( "dd-MMM-yyyy HH:mm:ss" );
    return DF.format( DT );
  catch ( Exception E ) {
    return "";
  }
 }

}
super( dateToString( new Date() ) ); it give me this error cant refrence this before super type constractur called
ASKER CERTIFIED SOLUTION
Avatar of arioh
arioh
Flag of Russian Federation image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thanks alot it works can i ask why it should be static
cause you can't invoke any class instance method before "super" constructor.
in case of static  - "dateToString" is not a class instance method, but a class method. You can invoke it from anywhere of you program without creating an instance of CurrentTime.