age calculator

age calculator

sir , actually m working on a project where i want the user to enter his date of birth in a jtextfield .and on doing so his age should b displayed in the next textfield automatically....plz help

View Answers

May 18, 2013 at 11:19 AM

hi friend,

Try this code, may this will be helpful for you.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class DateDifference implements ActionListener{
    JFrame f;
    JTextField dob = new JTextField();
    JTextField age = new JTextField();
    JButton r = new JButton("Reset");   
    JButton b= new JButton("Get Age");

    String startDateString = "01/01/2000";
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");    
    public void createUI()
    {
        f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        f.setLayout(null);
        JLabel dobLabel = new JLabel("Enter Your DOB : ");
        JLabel formatLabel = new JLabel("dd/MM/yyyy");
        JLabel formatLabel2 = new JLabel("yyyy/MM/dd");
        JLabel ageLabel = new JLabel("Age As On 01/01/2000 :");        
        b.addActionListener(this);
        r.addActionListener(this);

        dobLabel.setBounds(10, 30, 100, 20);
        dob.setBounds(150,30,150,20);
        formatLabel.setBounds(300,30,100,20);
        ageLabel.setBounds(10,60,130,20);
        age.setBounds(150,60,150,20);
        formatLabel2.setBounds(300,60,100,20);
        b.setBounds(90,110,120,20);
        r.setBounds(230, 110, 120, 20);

        f.add(dobLabel);
        f.add(dob);
        f.add(formatLabel);
        f.add(ageLabel);        
        f.add(age);
        f.add(formatLabel2);
        f.add(b);
        f.add(r);
        f.setVisible(true);
        f.setSize(400,300);
    }   

    public boolean isLeapYear(int year) {
        if (year % 4 != 0) {
        return false;
        } else if (year % 400 == 0) {
        return true;
        } else if (year % 100 == 0) {
        return false;
        } else {
        return true;
        }
        }

    public boolean isZero(String str)
    {
        String[] dobArr = str.split("/");
        int i = 0;
        while(i < dobArr.length)
        {
            int num = Integer.parseInt(dobArr[i]);
            //System.out.println(num);
            i++;
            if(num == 0)
            {
                return true;
            }
        }
        return false;       
    }
    public void calculateAndShowValue(int inDay, int stDay, int inMonth, int stMonth, int inYear, int stYear)
    {
        int day = inDay - stDay;
        int month = inMonth-stMonth;
        int year = inYear-stYear;
        String dayString = Integer.toString(day);
        String monthString = Integer.toString(month);
        String yearString = Integer.toString(year);
        String date="";     
            date = yearString+"/"+monthString+"/"+dayString;
        age.setText(date);
    }

    public static void main(String[] args){
        DateDifference dd = new DateDifference();
        dd.createUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {        
        //b = (JButton)e.getSource();       
        if(e.getSource() == b)
        {
            getOperation();
        }
        else if(e.getSource() == r)
        {
            dob.setText("");
            age.setText("");
        }       
    }

Continue......


May 18, 2013 at 11:22 AM

public void getOperation()
    {
        try{
        Date startDate = df.parse(startDateString);
        Calendar cal = Calendar.getInstance();        
        cal.setTime(startDate);

        String dobTextboxValue = dob.getText();
        boolean bol3 = isZero(dobTextboxValue);
        if(bol3 == true)
        {
            JOptionPane.showMessageDialog(f, "Date Shouldn't be Zero." +
                    " Try again.", "Error Message", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
        Date endDate = df.parse(dobTextboxValue);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endDate);
        if(endDate.compareTo(startDate)==0)
        {
            JOptionPane.showMessageDialog(f, "Both Date Are Same." +
                    " Try again.", "Error Message", JOptionPane.ERROR_MESSAGE);
        }
        else if(endDate.compareTo(startDate)<0)
        {
            JOptionPane.showMessageDialog(f, "Date Should Be After"+startDate +
                    " Try again.", "Error Message", JOptionPane.ERROR_MESSAGE);
        }
        else
        {           
            int inputYear = Math.abs(cal2.get(Calendar.YEAR));
            int startYear = Math.abs(cal.get(Calendar.YEAR));
            int inputMonth = Math.abs(cal2.get(Calendar.MONTH)+1);//because month started from 0
            int startMonth = Math.abs(cal.get(Calendar.MONTH)+1);
            int inputDay = Math.abs(cal2.get(Calendar.DAY_OF_MONTH));
            int startDay = Math.abs(cal.get(Calendar.DAY_OF_MONTH));            

            if(inputDay < startDay && (inputMonth == 1 || inputMonth == 3 || inputMonth == 5 || inputMonth == 7 || inputMonth == 8 || inputMonth == 10 || inputMonth == 12))
            {
                inputDay = inputDay+31;
                inputMonth = inputMonth-1;              
                if(inputMonth < startMonth)
                {
                    System.out.println(!(inputMonth <=0));
                    inputMonth = inputMonth+12;
                    inputYear = inputYear-1;

                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                    
                }
                else if(inputMonth >= startMonth)
                {
                    System.out.println("inputMonth : "+inputMonth+ "/"+!(inputMonth <=0));
                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                    
                }

            }

Continue....


May 18, 2013 at 11:23 AM

        else if(inputDay < startDay && (inputMonth == 2 || inputMonth == 4 || inputMonth == 6 || inputMonth == 9 || inputMonth == 11))
        {               
            if(inputMonth == 2)
            {
                boolean bol = isLeapYear(inputYear);                    
                if(bol == true)
                {
                    inputDay = inputDay+29;
                    inputMonth = inputMonth-1;                      
                    if(inputDay < startDay)
                    {
                        inputDay = inputDay+31;
                        inputMonth = inputMonth-1;
                        if(inputMonth < startMonth)
                        {
                            inputMonth = inputMonth+12;
                            inputYear = inputYear-1;

                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                                                
                        }
                        else if(inputMonth >= startMonth)
                        {
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                
                        }                           
                    }
                    else if(inputDay >= startDay)
                    {                           
                        if(inputMonth < startMonth)
                        {
                            inputMonth = inputMonth+12;
                            inputYear = inputYear-1;
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                                                
                        }
                        else if(inputMonth >= startMonth)
                        {
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                
                        }
                    }
                }
                else if(bol == false)
                {
                    inputDay = inputDay+28;
                    inputMonth = inputMonth-1;
                    if(inputDay < startDay)
                    {
                        inputDay = inputDay+31;
                        inputMonth = inputMonth-1;
                        if(inputMonth < startMonth)
                        {
                            inputMonth = inputMonth+12;
                            inputYear = inputYear-1;
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                
                        }
                        else if(inputMonth >= startDay)
                        {
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                
                        }                           
                    }
                    else if(inputDay >=startDay)
                    {
                        if(inputMonth < startMonth)
                        {
                            inputMonth = inputMonth+12;
                            inputYear = inputYear-1;
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                                            
                        }
                        else if(inputMonth >= startMonth)
                        {
                            calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                
                        }
                    }
                }
            }
            else
            {
                inputDay = inputDay+30;
                inputMonth = inputMonth-1;
                if(inputMonth < startMonth)
                {               
                    inputMonth = inputMonth+12;
                    inputYear = inputYear-1;
                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                        
                }
                else if(inputMonth >= startMonth)
                {
                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                        
                }
            }               
        }

Continue.....


May 18, 2013 at 11:24 AM

            else if(inputDay >= startDay)
            {
                if(inputMonth < startMonth)
                {
                    inputMonth = inputMonth+12;
                    inputYear = inputYear-1;
                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                                    
                }
                else if(inputMonth >= startMonth)
                {
                    calculateAndShowValue(inputDay, startDay, inputMonth, startMonth, inputYear, startYear);                    
                }
            }       
          }
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }    
}

Ads









Related Tutorials/Questions & Answers:
age calculator
age calculator  sir , actually m working on a project where i want the user to enter his date of birth in a jtextfield .and on doing so his age should b displayed in the next textfield automatically....plz help
Calculator
Calculator  need a simple java program to degin a CALCULATOR without using ADVANCED JAVA....   Calculator in Java Swing
Advertisements
age calculation
age calculation  pls send me the age calculation validation coding in java script. i'm waiting for ur reply
Average Age of my Class?
Average Age of my Class?  average age of my class
ModuleNotFoundError: No module named 'age'
ModuleNotFoundError: No module named 'age'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'age' How to remove the ModuleNotFoundError: No module named 'age' error
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Artifacts of com.clever-age
List of Artifacts of com.clever-age maven depenency
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
Calculator class
Calculator class  I am a beginner in Eclipse. I have to do a program called calculator that adds numbers. This is my code so far: //Margaret //ICS... class Calculator extends JFrame implements ActionListener { JTextField text
Calculator
Version of com.clever-age>play2-elasticsearch dependency
ModuleNotFoundError: No module named 'Calculator'
ModuleNotFoundError: No module named 'Calculator'  Hi, My Python... 'Calculator' How to remove the ModuleNotFoundError: No module named 'Calculator' error? Thanks   Hi, In your python environment you
thread program for calculator implementation
thread program for calculator implementation  Hi i'm prem i need calculator progrm in java that are implemented by Thread interface.....pls strong text
Is there any age limit for data scientist?
Is there any age limit for data scientist?  Hi, I am beginner... to learn: Is there any age limit for data scientist? Try to provide me good examples or tutorials links so that I can learn the topic "Is there any age limit
Calculator - JSP-Servlet
Calculator  Dear Deepak Sir, Calculator program is avilable in Jsp... calculator program in jsp function checkValue(){ var msg...; } Simple calculator program in jsp /> >
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
ModuleNotFoundError: No module named 'age-and-weight-Converter'
ModuleNotFoundError: No module named 'age-and-weight-Converter'  Hi...: No module named 'age-and-weight-Converter' How to remove the ModuleNotFoundError: No module named 'age-and-weight-Converter' error? Thanks  
ModuleNotFoundError: No module named 'mpi-age-aegis'
ModuleNotFoundError: No module named 'mpi-age-aegis'  Hi, My... 'mpi-age-aegis' How to remove the ModuleNotFoundError: No module named 'mpi-age-aegis' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age'  ...: No module named 'odoo10-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age'  ...: No module named 'odoo12-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo13-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo13-addon-hr-employee-age'  ...: No module named 'odoo13-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo13-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age'  ...: No module named 'odoo10-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo10-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo11-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo11-addon-hr-employee-age'  ...: No module named 'odoo11-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo11-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age'  ...: No module named 'odoo12-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-hr-employee-age' error
ModuleNotFoundError: No module named 'odoo12-addon-partner-age'
ModuleNotFoundError: No module named 'odoo12-addon-partner-age'  Hi...: No module named 'odoo12-addon-partner-age' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-partner-age' error? Thanks  
ModuleNotFoundError: No module named 'odoo12-addon-partner-age'
ModuleNotFoundError: No module named 'odoo12-addon-partner-age'  Hi...: No module named 'odoo12-addon-partner-age' How to remove the ModuleNotFoundError: No module named 'odoo12-addon-partner-age' error? Thanks  
ModuleNotFoundError: No module named 'odoo8-addon-hr-employee-age'
ModuleNotFoundError: No module named 'odoo8-addon-hr-employee-age'  ...: No module named 'odoo8-addon-hr-employee-age' How to remove the ModuleNotFoundError: No module named 'odoo8-addon-hr-employee-age' error? Thanks
ModuleNotFoundError: No module named 'aerospace-calculator'
ModuleNotFoundError: No module named 'aerospace-calculator'  Hi...: No module named 'aerospace-calculator' How to remove the ModuleNotFoundError: No module named 'aerospace-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'agnostic-calculator'
ModuleNotFoundError: No module named 'agnostic-calculator'  Hi, My... named 'agnostic-calculator' How to remove the ModuleNotFoundError: No module named 'agnostic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'arithmetic-calculator'
ModuleNotFoundError: No module named 'arithmetic-calculator'  Hi...: No module named 'arithmetic-calculator' How to remove the ModuleNotFoundError: No module named 'arithmetic-calculator' error? Thanks   Hi

Ads