## How can I find the number of years between two dates?

date calculator online

calculate years between two dates

how to calculate difference between two dates and times in excel

how to calculate years of service in excel

days between dates excel

date calculator between two dates

how to calculate number of days between two dates formula

I am trying to determine an age in years from a certain date. Does anyone know a clean way to do this in Android? I have the Java api available obviously, but the straight-up java api is pretty weak, and I was hoping that Android has something to help me out.

EDIT: The multiple recommendations to use Joda time in Android worries me a bit due to Android Java - Joda Date is slow and related concerns. Also, pulling in a library not shipped with the platform for something this size is probably overkill.

import java.util.Calendar; import java.util.Locale; import static java.util.Calendar.*; import java.util.Date; public static int getDiffYears(Date first, Date last) { Calendar a = getCalendar(first); Calendar b = getCalendar(last); int diff = b.get(YEAR) - a.get(YEAR); if (a.get(MONTH) > b.get(MONTH) || (a.get(MONTH) == b.get(MONTH) && a.get(DATE) > b.get(DATE))) { diff--; } return diff; } public static Calendar getCalendar(Date date) { Calendar cal = Calendar.getInstance(Locale.US); cal.setTime(date); return cal; }

**Date Duration Calculator: Days Between Dates,** If you want to calculated the number of years between two dates, you can use the YEARFRAC function, which will return a decimal number representing the If you want to calculated the number of years between two dates, you can use the YEARFRAC function, which will return a decimal number representing the fraction of a year between two dates.

I would recommend using the great Joda-Time library for everything date related in Java.

For your needs you can use the `Years.yearsBetween()`

method.

**Calculate the difference between two dates - Excel,** The YEARFRAC function returns the number of years elapsed between the two dates. It returns a decimal value. Syntax: =YEARFRAC(start_date, end_date). The Duration Calculator calculates the number of days, months and years between two dates.

**Years Between Dates Calculator: Years between two dates – DQYDJ,** The YEARFRAC function calculates years between two dates, including the fractional value. The decimal number represents the fraction of the year between the 1. Use DATEDIF to find the total years. In this example, the start date is in cell D17, and the end date is in E17. In the formula, the “y” returns the number of full years between the two days.

I apparently can't comment yet, but I think you can just use the DAY_OF_YEAR to workout if you should adjust the years down one (copied and modified from current best answer)

public static int getDiffYears(Date first, Date last) { Calendar a = getCalendar(first); Calendar b = getCalendar(last); int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR); if (a.get(Calendar.DAY_OF_YEAR) > b.get(Calendar.DAY_OF_YEAR)) { diff--; } return diff; } public static Calendar getCalendar(Date date) { Calendar cal = Calendar.getInstance(Locale.US); cal.setTime(date); return cal; }

Similarly you could probably just diff the ms representations of the time and divide by the number of ms in a year. Just keep everything in longs and that should be good enough most of the time (leap years, ouch) but it depends on your application for the number of years and how performant that function has to be weather it would be worth that kind of hack.

**Excel formula: Calculate years between dates,** Does anyone know the best way to calculate the number of year(s) as a single number "0, 1, 2" etc. between the current date and "Empl Svc Dt"? 2. Thanks for wanting to contribute. I tried your code with date1 = "07-11-2015"and date2 = "07-01-2016". Expected result was 2 months, Instead I got The Total Months difference between two dates is --> 22 Months. It’s easier than you think to get date math wrong, and even more so when you choose poor variable names.

I know you have asked for a clean solution, but here are two dirty once:

static void diffYears1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendar calendar1 = Calendar.getInstance(); // now String toDate = dateFormat.format(calendar1.getTime()); Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendar2.getTime()); // just simply add one year at a time to the earlier date until it becomes later then the other one int years = 0; while(true) { calendar2.add(Calendar.YEAR, 1); if(calendar2.getTimeInMillis() < calendar1.getTimeInMillis()) years++; else break; } System.out.println(years + " years between " + fromDate + " and " + toDate); } static void diffYears2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendar calendar1 = Calendar.getInstance(); // now String toDate = dateFormat.format(calendar1.getTime()); Calendar calendar2 = Calendar.getInstance(); calendar2.add(Calendar.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendar2.getTime()); // first get the years difference from the dates themselves int years = calendar1.get(Calendar.YEAR) - calendar2.get(Calendar.YEAR); // now make the earlier date the same year as the later calendar2.set(Calendar.YEAR, calendar1.get(Calendar.YEAR)); // and see if new date become later, if so then one year was not whole, so subtract 1 if(calendar2.getTimeInMillis() > calendar1.getTimeInMillis()) years--; System.out.println(years + " years between " + fromDate + " and " + toDate); }

**How to Calculate years between dates in Excel,** Calculate Number of Days, Weeks, Months and Years Between Two Dates in Excel Calculate Number of Days Between Two Dates Excel has a function that calculates the number of days between two dates, the DATEDIF function. The Formula =DATEDIF(A2,B2,"d") You got the number of days between two dates, but there is one problem, cell C4 returns an error.

**Excel formula: Calculate years between dates - Excelchat,** Calculate the Number of Weekends in Excel Between Two Dates. To calculate the number of weekends between 2 dates just insert the formula =SUM(INT((WEEKDAY(A2-{3,7})+B2-A2)/7)) where B2>A2. The following picture shows the result of the number of weekends between 2 dates. Add/Subtract Days to Date in Excel Excluding Weekends & Holidays

**How to calculate the number of years between two dates ,** The " yd ", and " md " interval codes yields the number of days between the two dates as if they were in the same year and ignores the year. To calculate the number of years, months and days between two dates (more than a year apart) you can use this formula (assuming your start date is in cell A1 and your end date is in cell A2).

**Calculate years between two dates in Excel,** Date Calculators. Duration Between Two Dates – Calculates number of days. Time and Date Duration – Calculate duration, with both date and time included. Date Calculator – Add or subtract days, months, years. Birthday Calculator – Find when you are 1 billion seconds old.

##### Comments

- A similar question: stackoverflow.com/questions/1116123/…
- Both the legacy
`Date`

/`Calendar`

classes and*Joda-Time*have been supplanted by the*java.time*classes. Most of the*java.time*functionality is back-ported to Java 6 & Java 7 in theproject. Further adapted for earlier Android in the*ThreeTen-Backport*project. See*ThreeTenABP**How to use ThreeTenABP…*. - What are the constants MONTH, YEAR and DATE ??
- @ChrisSim: They are static imports of Calendar.MONTH and so on.
- I think instead of DATE it should be DAY_OF_MONTH.
- @jiahao: they're synonyms
- .get(YEAR) and .get(MONTH) are now deprecated I believe. Possibly edit?
- I've used Joda a lot this last project. It is really over kill in terms of setting up...
- Just to elaborate:
`public int getYears(org.java.util.Date time) { org.joda.time.DateTime now = org.joda.time.DateTime.now(); org.joda.time.DateTime then = new org.joda.time.DateTime(time.getTime()); return org.joda.time.Years.yearsBetween(now, then).getYears(); }`

- Using DAY_OF_YEAR leads to errors with leap years too.
- There was a bug related to days being one-based. But this also doesn't work well with leap years. Please see my edit.
- Thanks for wanting to contribute. I tried your code with
`date1 = "07-11-2015"`

and`date2 = "07-01-2016"`

. Expected result was 2 months, Instead I got`The Total Months difference between two dates is --> 22 Months`

. It’s easier than you think to get date math wrong, and even more so when you choose poor variable names. There is a reason why people prefer and advocate the library classes. - There is more to calendaring than milliseconds. This code does not account for time zones, and so it ignores crucial issue of Daylight Saving Time (DST) and other anomalies. Nor does it account for Leap Year. Also, we now have classes for this work. Suggesting manual math for date-time handling is poor advice.
- @Johnny Graber care to explain why you upvoted this ... ehem ... suboptimal answer?
- Downvoted as this does not take the days into account.