-2

I have 2 Calendar objects for the current date and birthdate. I need to subtract birthdate from the current date and return the difference in years (a truncated Int is all I need).

What's the simplest way of doing this?

M-R
  • 411
  • 2
  • 6
  • 15
  • 1
    Just subtract the two years... simple enough. Look at the calendar class https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html `HINT: As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900` – 3kings May 05 '16 at 18:18
  • @But with your example I'd only be subtracting from the year, not the entire date. – M-R May 05 '16 at 18:26
  • You said you need the difference in years.... – 3kings May 05 '16 at 18:32
  • @3kings, Ahhh I meant the result would be in years. – M-R May 05 '16 at 18:37
  • @MartinRand Please search Stack Overflow before posting. Virtually all the basic date-time questions have already been asked and answered. – Basil Bourque May 05 '16 at 23:12

1 Answers1

0

You can get EPOCH time (miliseconds since 01.01.1900), subtract both values and divide it by 1000(miliseconds)/60(seconds)/60(minutes)/24(hours)/365(days). This would give you approximate result (in years) because of ignoring leap years.

To get epoch time, use Date#getTime();

Antoniossss
  • 31,590
  • 6
  • 57
  • 99