Swing-Java: Get current date and time using swing
Here is the example where you can get current time and date using frame in swing
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTimeDemo extends JFrame implements ActionListener{
public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
public static String dt;
JTextField dateField= new JTextField(20);
JButton jb=new JButton("Submit");
public DateTimeDemo() {
super("Current date and time demo");
JLabel label1 = new JLabel(" Your current time: ");
JPanel contentPane = new JPanel(new FlowLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPane.add(label1);
contentPane.add(dateField);
contentPane.add(jb);
jb.addActionListener(this);
getContentPane().add(contentPane);
}
public void actionPerformed(ActionEvent e) {
String date = DateTime();
dateField.setText(date);
JOptionPane.showMessageDialog(null,"Current date and time: "+date+"!");
}
public static void main(String[] argv) {
DateTimeDemo f = new DateTimeDemo();
f.setSize(300,200);
f.setLocation(200,200);
f.setDefaultCloseOperation( EXIT_ON_CLOSE );
f.setVisible(true);
}
public static String DateTime(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTimeDemo extends JFrame implements ActionListener{
public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";
public static String dt;
JTextField dateField= new JTextField(20);
JButton jb=new JButton("Submit");
public DateTimeDemo() {
super("Current date and time demo");
JLabel label1 = new JLabel(" Your current time: ");
JPanel contentPane = new JPanel(new FlowLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentPane.add(label1);
contentPane.add(dateField);
contentPane.add(jb);
jb.addActionListener(this);
getContentPane().add(contentPane);
}
public void actionPerformed(ActionEvent e) {
String date = DateTime();
dateField.setText(date);
JOptionPane.showMessageDialog(null,"Current date and time: "+date+"!");
}
public static void main(String[] argv) {
DateTimeDemo f = new DateTimeDemo();
f.setSize(300,200);
f.setLocation(200,200);
f.setDefaultCloseOperation( EXIT_ON_CLOSE );
f.setVisible(true);
}
public static String DateTime(){
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
}
thanks
ReplyDeletejhidzzao@gamil.com
i need LIBRARY SYSTEM using JAVA
i dont know how to code
huhu
entity
LOGIN
BOOKS
BORROWER
BORROWEDBOOKS
RESERVEBOOKS
RETURNBOOK