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