-1

I am trying to make a method that will calculate the age of a person. I want the calculation to be done under the second public static int getAge. If the person is born after the current date i want it to print out error -1.

How do I compare the two SimpleDate values dateBd and dateRef in order to get an int value for age?

public static SimpleDate today() {


Calendar todayCal = Calendar.getInstance();
SimpleDate todayDate = new SimpleDate();


todayDate.setDate(todayCal.get(Calendar.MONTH) + 1,  
                  todayCal.get(Calendar.DATE),
                  todayCal.get(Calendar.YEAR));
return todayDate;

public static int getAge(SimpleDate dateBd) {
int age;
SimpleDate dateToday = today();


age = getAge(dateBd, dateToday);  
return age;

public static int getAge(SimpleDate dateBd, SimpleDate dateRef) {

if(getAge(dateBd)>getAge(dateRef)){
system.out.println("error");
}
return -1;
Michael Wood
  • 25
  • 1
  • 4
  • Either use JodaTime or Java 8's Time API, for [example](http://stackoverflow.com/questions/28979880/about-days-between-two-dates/28980026#28980026) and [example](http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021#12852021) – MadProgrammer Apr 19 '15 at 23:41
  • 1
    The answer to any remotely date or time related question in Java is "use JodaTime" by definition. – Alex Apr 19 '15 at 23:47
  • You can also use a `PeriodFormatter` from JodaTime, as demonstrated [here](http://stackoverflow.com/questions/29664907/display-hours-between-2-dates-in-java/29665202#29665202) – MadProgrammer Apr 19 '15 at 23:48
  • What is the `SimpleDate` class? We cannot help you with unexplained classes. – Basil Bourque Apr 20 '15 at 02:01

2 Answers2

0

What is SimpleDate ? Anyway here something to get you started

 import java.util.GregorianCalendar;
 import java.util.Calendar;

 public class CalcAge {

   public static void main(String [] args) {
     // remember ... months are 0-based : jan=0 feb=1 ...
     System.out.println
       ("1962-11-11 : " + age(1962,10,11));
     System.out.println
       ("1999-12-03 : " + age(1999,11,3));
   }

   private static int age(int y, int m, int d) {
     Calendar cal = new GregorianCalendar(y, m, d);
     Calendar now = new GregorianCalendar();
     int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
     if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
       || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)
       && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH)))
     {
        res--;
     }
     return res;
   }
} 
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
0

Don't ever try and use the millisecond difference between two times to calculate the differences, there are just to many idiosyncrasies with date/time calculations which can cause all sorts of erroneous errors.

Instead, save yourself (alot) of time and use a dedicated library

Java 8

LocalDate start = LocalDate.of(1972, Month.MARCH, 8);
LocalDate end = LocalDate.now();

long years = ChronoUnit.YEARS.between(start, end);
System.out.println(years);

Which outputs 43

JodaTime

DateTime startDate = new DateTime(1972, DateTimeConstants.MARCH, 8, 0, 0);
DateTime endDate = new DateTime();

Years y = Years.yearsBetween(startDate, endDate);
int years = y.getYears();
System.out.println(years );

Which outputs 43

You can even use a Period to gain more granuarlity...

    Period period = new Period(startDate, endDate);
    PeriodFormatter hms = new PeriodFormatterBuilder()
                    .printZeroAlways()
                    .appendYears()
                    .appendSeparator(" years, ")
                    .appendMonths()
                    .appendSeparator(" months, ")
                    .appendDays()
                    .appendLiteral(" days")
                    .toFormatter();
    String result = hms.print(period);
    System.out.println(result);

Which prints 43 years, 1 months, 5 days

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366