How do i set Date and display in Jtextfield ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i set Date and display in Jtextfield ?

Java

10th May 2019, 6:42 AM
Daryll Culas
Daryll Culas - avatar
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()); } }
10th May 2019, 9:15 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 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(); } }
10th May 2019, 12:14 PM
JavaBobbo
JavaBobbo - avatar