Example : How to choose the Date or Create Calendar box in NetBeans 8.2 for a Java application?
Step1: Download 'jcalendar-1.4.jar' file first.

Step2: Now, Add Jar File to Project - For this, Right-click your Project name -
Select 'Properties' - Click 'Libraries' - Click 'Add JAR/Folder' - Select 'jcalendar-1.4.jar' file from their storage location - Click OK.

Step3: Now, Add 'JDateChooser' to Palette - For this, Open Tools - Palette - 'Swing/AWT Components' - Click 'Add from JAR' - Select 'jcalendar-1.4.jar' - Select
'JDateChooser' - Click 'Next' - select 'Swing controls' - Finish - Restart the Netbeans 8.2.

Now JDateChooser will appear in the Palette - Drag 'JDateChooser' onto your form.

------------  Get Selected Date from jDateChooser  --------------

   java.util.Date dob = jDateChooser1.getDate();
   System.out.println(dob);

------------  Convert Date to String from jDateChooser  --------------

import java.text.SimpleDateFormat;

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

String dob =sdf.format(jDateChooser1.getDate());
System.out.println(dob);

------------  Reset Date of jDateChooser  --------------

jDateChooser1.setDate(null);
Example : How to display various date formats 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 a 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.