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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


saurabh singh
age calculator
4 Answer(s)      a year and 5 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
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 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
 

 

 

DMCA.com