1

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

enter image description here

This is the picture from a website age calculator Where I've taken input as birth date: 22 March 1999

And

enter image description here

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
CSE
  • 73
  • 6
  • 5
    Why not just use `ChronoUnit.DAYS.between(dob, end)` which returns `8767`? – Nexevis Mar 22 '23 at 18:48
  • 2
    Your 30.433 is nowhere near accurate enough. – Dawood ibn Kareem Mar 22 '23 at 18:52
  • As an aside converting an integer to string (using `String.valueOf()`) and back to an integer (using `Integer.parseInt()`) is redundant and needless. – Ole V.V. Mar 22 '23 at 19:27
  • 3
    You will get closer but not really close if you use `ChronoUnit.MONTHS.getDuration()`. It says that 288 months equal 8765 days 19 hours 40 minutes 48 seconds. Best to use `ChronoUnit.DAYS.between(dob, end)` as @Nexevis says. It takes into account that months have different lengths and uses the actual months passed including taking leap years into account. – Ole V.V. Mar 22 '23 at 19:33

1 Answers1

2

I've Just Made One Changes As per above comments of @Nexevis and @Ole V.V.

I Wrote

long totalDay = ChronoUnit.DAYS.between(dob, end);

Instead of

convertDays = (totalMonths * 30.433);
totalDays = (int)convertDays + Integer.parseInt(String.valueOf(period.getDays()));

And Leave Everything as like before. I've Just use ChronoUnit method to find out total days. And use Period to find out actual age.

CSE
  • 73
  • 6