Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


saurabh singh
age calculator
4 Answer(s)      a year and 3 months ago
Posted in : Java Beginners


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


Advertisement
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
Average Age of my Class?
Average Age of my Class?  average age of my class
calculator midlet
calculator midlet  give me code calculator midlet in bluetooth application with j2me
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
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
simple calculator program in javascript
simple calculator program in javascript  strong textsimple calculator in javascript
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
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
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
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
PHP Tax Calculator - PHP
PHP Tax Calculator  In my project i required a tax calculator that can calculate the property tax
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 /> >
calculator - Java Server Faces Questions
calculator  Some ideas for the project of calculator
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
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
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
calculator in java with stack
calculator in java with stack  i want calcultor with interface in java and in interface there is button called postfix ,,, when the user enter opertions and numbers first check if is vaild or not then convert to postfix
base calculator.. - Java Beginners
base calculator..  need some help!!! I cant install jcreator properly?? there's always an error (Error : Invalid path, "C:\Program Files\Java\jre1.6.0_01\bin\javac.exe" -classpath "C:\Program Files\Java\jdk1.6.0_01\bin" -d "C
JavaScript calculate age from date of birth
JavaScript calculate age from date of birth  How to calculate age...(); //getting dob year var age = y1 - y2; //calculating age document.write("Age : " + age); return true; } else
Java Calculator Program
Java Calculator Program  Hi, so I need to make a program that "works like a calculator". I need to make two versions: 1) I'm given the Expression Class and need to implement the children classes, which are Number, Product, Sum
Simple Calculator Application In Java Script
Simple Calculator Application In Java Script  ... Calculator The objective of this project is  learn how to write a simple calculator with the JavaScript programming language. You will learn how to write
Design and Implement GPA Calculator
Calculator"); //frame.setLayout(new BorderLayout()); frame.setLayout...); headingLabel = new JLabel("Welcome To GPA Calculator
swimming pool calculator
", customers); jtabbedPane.addTab("Temp Calculator", tempCalc
Simple Java Calculator - Java Beginners
Calculator for 4 basic Math operations, Addition, Subtraction, Multiplication and Division. The calculator should simulate the look of handheld calculator containing.... Write proper code for making this calculator operational. Students
Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main (Calc.java) - A simple main program. User interface (CalcGUI.java
Java Calculator program - Java Beginners
Java Calculator program  import java.awt.*; import javax.swing.*; import java.awt.event.*; class Calculator implements ActionListener { int c,n...; Calculator() { f=new Frame("Calculator"); p=new Panel(); b1=new JButton("0
Calculate Age using current date and date of birth
Calculate Age using current date and date of birth In this section you will learn how to calculate the age. For this purpose, through the code, we have... to determine the age, we have used the following code
Java: Example - Simple Calculator
Java: Example - Simple Calculator Here is the source for the simple calculator shown at the left. It's divided into three source files. Main...*; ///////////////////////////////////////////////////////////// class Calc /** Calc.java - A simple calculator. @author Fred
HTML Calculator
HTML Calculator In this example we will learn how to make HTML Calculator. We have used HTML language in this example. First off all we have created one...;. This calculator can be used to perform simple mathematical operations like
Determining the actual age from date of birth in Java
Determining the actual age from date of birth in Java... the age of a person easily through the given program. This program takes... age which determines the years, months and days of the calculated age
Swimming Pool Calculator - Java Beginners
Calculator is one that is very simple to use. As you can see, it is a user... PoolVolume() { mainFrame = new JFrame("Swimming Pool Volume Calculator
Writing Calculator Stateless Session Bean
Writing Calculator Stateless Session Bean... for our Calculator Session Bean:   /* * ...; <session > <description>EJB Calculator Session
Java Swing Scientific Calculator
Java Swing Scientific Calculator A Scientific Calculator is a very powerful and general purpose calculator. In addition to basic arithmetic functions... calculator using java swing. Here is the code: import java.awt.*; import
sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..
sql query to get name,age,phone,address from tables t1,t2,t3,t4,t5 ..   this is for single table : query ="SELECT name , age , pwd , phone... by name"; i want for 12 tables : i want to fetch name age pwd phone address from 12
java loan calculator applet help
java loan calculator applet help  Hi, I could use some help correcting a code here. I need to write a Java applet program (together with its html test file) to calculate loan payments. The user will provide the interest rate
Swimming Pool Calculator - Java Beginners
()); mainFrame = new JFrame("Swimming Pool Volume Calculator"); calcButton = new
Swimming Pool Calculator - Java Beginners
Calculator", tempCalc ); jtabbedPane.addTab( "Customers", customers
how can you calculate you your age in daies??
how can you calculate you your age in daies??  **hi, I am beginner in java! can any one help me to write programm to calculate age in daies... * 1000); System.out.println("Your Age is: " + diffDays + " days
Calculator program in Java
Calculator program in Java is used by programmer to add, subtract, multiply... a class "calculator" is used. System.in takes the input from the system/user at run... other wise it will be a fraction value) Example of Calculator program in Java
iPhone EMI Calculator, EMI Calculator for iPhone
EMI Calc - iPhone EMI Calculator     EMI Calc... of an EMI Calculator with the tool name of EMI Calc. The EMI Calc provides you... financial institution. The EMI Calculator tab of EMI Calc calculates the exact
prog. using radio buttons for simple calculator
*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font... = new CalculatorOp(); public Calculator() { textfield = new JTextField("0...(pan); this.pack(); this.setTitle("Calculator
prog. using radio buttons for simple calculator
*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font... = new CalculatorOp(); public Calculator() { textfield = new JTextField("0...(pan); this.pack(); this.setTitle("Calculator
Writing Calculator Program in Swing
Writing Calculator Program in Swing   ... illustrates you how to a create calculator in Swing with the source code and screen shot. For developing a small calculator program in swing we need two different
 

 

 

DMCA.com