cbsecsnip-logo                         cbse-syllabus
cbse 100%
An online library for CBSE Computer Science and Informatics Practices students

Code for finding Age in Days Months Years formatPrint Friendly Version of this pagePrint Get a PDF version of this webpagePDF

Hi All

We all know we don’t have calendar control in AWT API or in Swing API. Which make very difficult to design any application in Java, if we want to use calendar for date manipulation. Well! We can do it in other way.

If you want to keep calendar in your Java GUI Application using NetBeans then here is a small tutorial for fulfilling your desire, you can download the complete project for age calculation. Here I am presenting one simple Application using JCalendar control for calculating the age of a person or anything. Figure – 1 is the output of the application

fig-1  Figure - 1

Steps for developing Age Calculator using Java:

a.   You will need to download the JCalendar package from here http://www.cbsecsnip.in/cbse-cs-n-ip/software.php. This package is originally developed by www.toedter.com, thanks to them who gave us a very useful control.

b.   After downloading and opening the rar file, you will get multiple sets of folders. The file which is important for us is ‘jcalendar-1.4.jar’, which is inside ‘lib’ folder. See the Figure – 2 and Figure – 3.
fig-2  Figure - 2
fig-3  Figure - 3

c.   Now create a New GUI Project in NetBeans and design it as shown in figure – 4
fig-4  Figure - 4

d.   For adding calendar control on frame, you have add JCalendar-1.4.jar in you Palette window. You can add JCalendar by right click on palette window you will get a context menu, at the bottom of the menu you can see Palette Manager… tab, click on Palette Manager tab. See figure – 5
fig-5  Figure - 5


e.   Next step is to extracted JCalendar jar file in palette list. Follow the figures –
fig-e1   Figure – e.1 Click on Add From JAR… button

fig-e2Select the Extracted Jar file jcalender-1.4 as shown in figure and click on Next button. Figure – e.2  
Figure – e.2

Now select the JDateChooser from the list (here I am using JDateChooser in my application) you can select any 1 as per your requirement aand click on Next button. Fig – e.3
fig-e3   Figure – e.3

Select the category in which you want to add the control from the shown list and just click on Finish button as shown in figure. Fig – e.4
fig-e4   Figure – e.4

And finally close the Palette Manager window.
Now JDateChooser is added in your Palette window in category which you select in Fig – e.4.
f.   Drag and Drop 2 JDateChooser controls and 3 JTextField controls on you JFrame.
g.   Unselect the enabled property of JTextField controls, so the value cannot be modified by user.
h.   Now open the Source Code window and start writing the code.
Inside the ageCalculation() Method given below-

public void ageCalculation()
    {
       Date dt1=jDateChooser1.getDate();
       Date dt2=jDateChooser2.getDate();
       fromDate=Calendar.getInstance();
       toDate=Calendar.getInstance();
       if(dt1.after(dt2))
       {
           fromDate.setTime(dt2);
           toDate.setTime(dt1);
       }   
       else 
       {
           fromDate.setTime(dt1);
           toDate.setTime(dt2);
       }
       
       increment=0;
       ///
       ///day calculation
       ///
       if(fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH))
       {
           increment=monthDay[fromDate.get(Calendar.MONTH)];
       }
       if (increment == -1)
       {
           if(fromDate.getActualMaximum(Calendar.DAY_OF_MONTH)==29)
           {
               increment=29;
           }
           else
           {
               increment=28;
           }
       }
       if (increment != 0)
       {
           ageDays = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH);
           increment = 1;
       }
       else
       {

           ageDays= toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH);
       }
       ///
       ///month calculation
       ///
       if ((fromDate.get(Calendar.MONTH)+increment ) > toDate.get(Calendar.MONTH))
       {
            int mcal=fromDate.get(Calendar.MONTH)+increment;
            toDate.add((Calendar.MONTH)+12, - mcal);
            ageMonths=toDate.get(Calendar.MONTH);
            increment = 1;
       }
       else
       {
            int mcal=fromDate.get(Calendar.MONTH)+increment;
            toDate.add(Calendar.MONTH, - mcal);
            ageMonths=toDate.get(Calendar.MONTH);
            increment = 0;
       }
       ///
       /// year calculation
       ///
       ageYear = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);
       
       ///
       /// display age in year, month,day
       ///
       
       jTextField1.setText(Integer.toString(ageYear));
       jTextField2.setText(Integer.toString(ageMonths));
       jTextField3.setText(Integer.toString(ageDays));

    }
For calling the method ageCalculation( ), write the following statement -
ageCalculation();
Inside private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ }i You can get this method can by double clicking on Button.
You need to import some package at the top of the code –
import javax.swing.ImageIcon;
import java.util.Calendar;
import java.util.Date;

You have declare few array, variables and objects at the class level, which are –
int monthDay[] = { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int ageDays;int ageMonths;int ageYear;
Calendar fromDate;Calendar toDate;
int increment;
While constructor initialize the object and components at that time you have to set the format and default date for JDateCooser control by writing these statements –
jDateChooser1.setDateFormatString("dd-MM-yyyy");
jDateChooser2.setDateFormatString("dd-MM-yyyy");
jDateChooser1.setDate(Calendar.getInstance().getTime());
jDateChooser2.setDate(Calendar.getInstance().getTime());

After completing all steps as mentioned in this tutorial execute the program and calculate the Age of person in all tense (Past, Present, Future).
If you have any question regarding this tutorial feel free to contact us at cbsecsnip.in.

Regards
Vishal Deb
Author

Receive all updates via Facebook. Just Click the Like Button Below...

Powered By EXEIdeas

Copyright © 2012, cbsecsnip.in | Contact Us | Disclaimer | | Who links to my website? | Privacy Policy | Site Map