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



 }

Comments

  1. thanks
    jhidzzao@gamil.com
    i need LIBRARY SYSTEM using JAVA
    i dont know how to code

    huhu

    entity
    LOGIN
    BOOKS
    BORROWER
    BORROWEDBOOKS
    RESERVEBOOKS
    RETURNBOOK

    ReplyDelete

Post a Comment

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Read Excel using Apache POI - Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'