I want to make an age calculator. What I want here is to show the current age and convert that age to total months and days and convert those whole months and days together to total days.
But, I am not getting the correct result here. I get different results with the same input in the online age calculator, different results with the same input in the age calculator I made.
I've provided Screenshot for better understanding
This is the picture from a website age calculator Where I've taken input as birth date: 22 March 1999
And
This is the age calculator I have made.
The age calculator on the website shows the total days as 8767 days and the calculator I made shows the days as 8765 days.
My Code For Calculation:
int date1, year1, date2, year2, convertMonth, totalMonths, totalDays,
double convertDays;
date1 = Integer.parseInt(dateBox.getSelectedItem().toString());
year1 = Integer.parseInt(yearField.getText());
date2 = Integer.parseInt(atDateBox.getSelectedItem().toString());
year2 = Integer.parseInt(atYearField.getText());
/*-----------------Calculate Current Age----------------------------*/
LocalDate dob = LocalDate.of(year1, birthMonth, date1);
LocalDate end = LocalDate.of(year2, currentMonth, date2);
Period period = Period.between(dob, end); //calculates the difference between two dates
/*---------------------------Starting conversion-------------------------------*/
//Convert year,month,days to month, days;
convertMonth = Integer.parseInt(String.valueOf(period.getYears())) * 12;
totalMonths = convertMonth + Integer.parseInt(String.valueOf(period.getMonths()));
//Convert month,days to total days
convertDays = (totalMonths * 30.433);
totalDays = (int)convertDays + Integer.parseInt(String.valueOf(period.getDays()));
/*---------------------------End conversion-------------------------------*/
lblShowAns.setText(String.valueOf(period.getYears()) + " Years " + String.valueOf(period.getMonths()) + " Months " + String.valueOf(period.getDays()) + " Days ");
lblShowMD.setText(String.valueOf(totalMonths) + " Months " + String.valueOf(period.getDays()) + " Days ");
lblShowDate.setText(String.valueOf(totalDays) + " Days ");
Note: I've Declared birthMonth
and currentMonth
variables globally and take these two input from another block of code.