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



 }

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Android: where to run keytool command in android