Example : How to display various format of Date in Java.
import java.util.Date;
import java.text.Format;
import java.text.SimpleDateFormat;
public RegDate() 
{
        initComponents();        
            this.setExtendedState(this.MAXIMIZED_BOTH);   //To maximize the jFrame.
       
            jXDatePicker1.setDate(new Date());
            jXDatePicker1.setFormats(new String[]{"E yyyy.MM.dd 'at' hh:mm:ss a zzz"});
            jXDatePicker1.setFormats(new String[]{"E yyyy-MM-dd 'at' hh:mm:ss a zzz"});
            Output : 
            Thu 2019.10.17 at 12:00:00 AM IST
            Thu 2019-10-17 at 12:00:00 AM IST            

            ----------  OR  ----------
      
            jXDatePicker1.setDate(new Date());
            jXDatePicker1.setFormats(new String[]{"dd-MMM-yyyy"});

            Output :
            17-Oct-2019
            
            ----------  OR  ----------
            jXDatePicker1.setDate(new Date());
            jXDatePicker1.setFormats(new String[]{"dd-MMMM-yyyy"});

            Output :
            17-October-2019
            
            ----------  OR  ----------
            jXDatePicker1.setDate(new Date());
            jXDatePicker1.setFormats(new String[]{"E dd-MMM-yyyy"});

            Output :
            Thu 17-Oct-2019

            ----------  OR  ----------
            jXDatePicker4.setDate(new Date());
            jXDatePicker4.setFormats(new String[]{"EEEE dd-MMMM-yyyy"});

            Output :
            Thursday 17-October-2019

            ----------  OR  ----------
            Date dNow =new Date();
            SimpleDateFormat tt = new SimpleDateFormat("hh:mm:ss a");
            jTextField1.setText(tt.format(dNow));

            Output :
            12:10:05 AM    //Static Time display       
}
Example : How to display dynamic time as watch in Java.
public void showDate()
{        
        new Timer(0, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Date dt =new Date();
                SimpleDateFormat s = new SimpleDateFormat("hh:mm:ss a");
                jTextField1.setText(s.format(dt));
            }
        }).start();
}

Loading

Categories: Java Project

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.