0
How do i set Date and display in Jtextfield ?
Java
2 Answers
+ 1
Something like :
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class TestTextField{
public static void main( String[] args ) {
JFrame testFrame = new JFrame( "Test Frame" );
JTextField jTextField3 = new JTextField();
testFrame.setSize( 150, 30 );
jTextField3.setText( now() );
testFrame.add( jTextField3 );
testFrame.setVisible( true );
}
public static final String DateFormat = "HH:mm dd/MM/yyyy";
public static String now(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat(DateFormat);
return format.format(cal.getTime());
}
}
+ 1
Another way you can do it:
import java.awt.*;
import java.time.*;
import javax.swing.*;
public class DateClass{
LocalDate date = LocalDate.now();
// Constructor
DateClass(){
JFrame frame = new JFrame();
JTextField tf = new JTextField(10);
tf.setText(" "+date);
frame.add(tf);
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.setSize(300,300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main( String[] args ) {
new DateClass();
}
}