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









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
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
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
compare age with dob - WebSevices
compare age with dob  Thanks for your quick reply. but I need exact... as Age like(18). but we dont have specified age field for fetching. instead of age field we have dateofbirth field(YYYY-MM-DD). so how we compare age
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
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
Artifacts of com.clever-age
List of Artifacts of com.clever-age maven depenency
Calculator
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
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
base calculator.. - Java Beginners
base calculator..  Help, i need some help about a base calculator.. i don't know how to start
Version of com.clever-age>play2-elasticsearch dependency
calculator - Java Interview Questions
calculator  create calculator by java code  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
Program for Calculator - Swing AWT
Program for Calculator  write a program for calculator?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Hope that it will be helpful
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
simple calculator - Java Beginners
simple calculator  how can i create a simple calculator using java codes?  Hi Friend, Please visit the following link: http://www.roseindia.net/java/example/java/swing/calculator-in-swing.shtml Thanks
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 /> >
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
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
Scientific Calculator - Java Beginners
Scientific Calculator  Develop a scientific calculator using even-driven programming paradigm of Java.? Thanks in ADVANCE  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java
matrix calculator - Java Beginners
matrix calculator  hi..... can you help me in writing source code of matrix calculator in java... i know you are the best you can do it!!! show yourself
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 '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-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 '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 '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 '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 '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 'currency-calculator'
ModuleNotFoundError: No module named 'currency-calculator'  Hi, My... named 'currency-calculator' How to remove the ModuleNotFoundError: No module named 'currency-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'Dewpoint-Calculator'
ModuleNotFoundError: No module named 'Dewpoint-Calculator'  Hi, My... named 'Dewpoint-Calculator' How to remove the ModuleNotFoundError: No module named 'Dewpoint-Calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'distances-calculator'
ModuleNotFoundError: No module named 'distances-calculator'  Hi...: No module named 'distances-calculator' How to remove the ModuleNotFoundError: No module named 'distances-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'engineering-calculator'
ModuleNotFoundError: No module named 'engineering-calculator'  Hi...: No module named 'engineering-calculator' How to remove the ModuleNotFoundError: No module named 'engineering-calculator' error? Thanks   Hi
ModuleNotFoundError: No module named 'fbp_calculator'
ModuleNotFoundError: No module named 'fbp_calculator'  Hi, My... named 'fbp_calculator' How to remove the ModuleNotFoundError: No module named 'fbp_calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'jas-calculator'
ModuleNotFoundError: No module named 'jas-calculator'  Hi, My... named 'jas-calculator' How to remove the ModuleNotFoundError: No module named 'jas-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'jas-calculator'
ModuleNotFoundError: No module named 'jas-calculator'  Hi, My... named 'jas-calculator' How to remove the ModuleNotFoundError: No module named 'jas-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'loan-calculator'
ModuleNotFoundError: No module named 'loan-calculator'  Hi, My... named 'loan-calculator' How to remove the ModuleNotFoundError: No module named 'loan-calculator' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'matrix-calculator'
ModuleNotFoundError: No module named 'matrix-calculator'  Hi, My... named 'matrix-calculator' How to remove the ModuleNotFoundError: No module named 'matrix-calculator' error? Thanks   Hi, In your

Ads