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


  public void actionPerformed(ActionEvent e) {

     String date = DateTime();
     JOptionPane.showMessageDialog(null,"Current date and time: "+date+"!");

   public static void main(String[] argv) {
         DateTimeDemo f = new DateTimeDemo();
         f.setDefaultCloseOperation( EXIT_ON_CLOSE );

   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

Android: where to run keytool command in android

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