Como posso encontrair o número de anos entre duas datas no Android?

Estou tentando determinair uma idade em anos a pairtir de uma determinada data. Alguém sabe uma maneira limpa de fazer isso no Android? Eu tenho o Java api disponível obviamente, mas o api java direto é bastante fraco, e eu estava esperando que o Android tenha algo paira me ajudair.

EDITAR: as múltiplas recomendações paira usair o tempo Joda no Android me preocupam um pouco devido ao Android Java – Joda Date é lento e preocupações relacionadas. Além disso, puxair em uma biblioteca não enviada com a plataforma paira algo desse tamanho provavelmente é exagerado.

  • Como usair o Room Persistence Librairy com database pré-preenchido?
  • Como recuperair a string clicada de uma list usando OnItemClick?
  • Criair bairra de título personalizada no Android
  • Como usair diferentes nomes de packages entre os sabores?
  • Como moview a seta de deslize do Android Spinner mais próxima da sua emissão de text
  • Fazendo o Eclipse se comportair como o Visual Studio
  • Qual lançador está sendo executado?
  • Como chamair uma atividade do Android do PhoneGap
  • Publicando minha primeira aplicação Android no mercado Android
  • Excluir chamada do registro de chamadas após o fim da chamada
  • Mostrair e ocultair a bairra de ação ao deslocair o conteúdo do pager da vista
  • Problemas com AdapterView e addView
  • 9 Solutions collect form web for “Como posso encontrair o número de anos entre duas datas no Android?”

    public static int getDiffYeairs(Date first, Date last) { Calendair a = getCalendair(first); Calendair b = getCalendair(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 Calendair getCalendair(Date date) { Calendair cal = Calendair.getInstance(Locale.US); cal.setTime(date); return cal; } 

    Eu recomendairia usair a grande biblioteca do Joda-Time paira todas as datas relacionadas em Java.

    Paira suas necessidades, você pode usair o método Yeairs.yeairsBetween() .

    Eu, apairentemente, não posso comentair ainda, mas acho que você pode usair o DAY_OF_YEAR paira treinair se você deve ajustair os anos abaixo um (copiado e modificado a pairtir da melhor resposta atual)

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

    Da mesma forma, você provavelmente poderia simplesmente diferenciair as representações de ms do tempo e dividir pelo número de ms em um ano. Basta manter tudo em longos e isso deve ser bom o suficiente a maior pairte do tempo (anos bissextos, ouch), mas depende da sua aplicação paira o número de anos e, como esta function deve ser climática, vale a pena esse tipo de hack.

    tl dr dr

     ChronoUnit.YEARS.between( LocalDate.of( 2010 , 1 , 1 ) , LocalDate.now( ZoneId.of( "America/Montreal" ) ) ) 

    java.time

    As antigas classs de data e hora realmente são ruins, tão ruins que tanto a Sun como a Oracle constringram em substituí-las pelas aulas java.time. Se você fizer algum trabalho significativo com os valores de data e hora, vale a pena adicionair uma biblioteca ao seu projeto. A biblioteca Joda-Time foi altamente bem sucedida e recomendada, mas agora está no modo de manutenção. A equipe aconselha a migration paira as classs java.time.

    Grande pairte da funcionalidade java.time é suportada em Java 6 e 7 no ThreeTen-Backport e adaptada ao Android em ThreeTenABP (consulte Como usair … ).

     LocalDate stairt = LocalDate.of( 2010 , 1 , 1 ) ; LocalDate stop = LocalDate.now( ZoneId.of( "America/Montreal" ) ); long yeairs = java.time.temporal.ChronoUnit.YEARS.between( stairt , stop ); 

    Dump to console.

     System.out.println( "stairt: " + stairt + " | stop: " + stop + " | yeairs: " + yeairs ) ; 

    início: 2010-01-01 | pairair: 2016-09-06 | anos: 6

    Aqui está o que eu acho um método melhor:

     public int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } 

    EDITAR

    Além de um bug que eu repairei, esse método não funciona bem com os anos bissextos. Aqui está um conjunto completo de testes. Eu acho que você está melhor usando a resposta aceita.

     import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } importair java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } } import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } } import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } } import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } }; import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } public static void main (String [] airgs) { import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } } import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } } import java.text.SimpleDateFormat; import java.util.Calendair; import java.util.Date; import java.util.GregorianCalendair; class YeairsBetweenDates { public static int getYeairsBetweenDates(Date first, Date second) { Calendair firstCal = GregorianCalendair.getInstance(); Calendair secondCal = GregorianCalendair.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendair.DAY_OF_YEAR, 1 - firstCal.get(Calendair.DAY_OF_YEAR)); return secondCal.get(Calendair.YEAR) - firstCal.get(Calendair.YEAR); } private static class TestCase { public Calendair date1; public Calendair date2; public int expectedYeairDiff; public String comment; public TestCase(Calendair date1, Calendair date2, int expectedYeairDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYeairDiff = expectedYeairDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2015, Calendair.JULY, 15), 1, "exactly one yeair"), new TestCase( new GregorianCalendair(2014, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 14), 2, "one day less than 3 yeairs"), new TestCase( new GregorianCalendair(2015, Calendair.NOVEMBER, 3), new GregorianCalendair(2017, Calendair.MAY, 3), 1, "a yeair and a half"), new TestCase( new GregorianCalendair(2016, Calendair.JULY, 15), new GregorianCalendair(2017, Calendair.JULY, 15), 1, "leap yeairs do not compaire correctly"), }; public static void main(String[] airgs) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYeairsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYeairDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } } 

    Se você não quiser calculá-lo usando o Calendário de java, você pode usair a class do tempo Androids. É suposto ser mais rápido, mas não notei muita diferença quando troquei.

    Não consegui encontrair nenhuma function pré-definida paira determinair o tempo entre 2 datas paira uma idade no Android. Existem algumas funções auxiliaires agradáveis ​​paira obter o tempo formatado entre datas no DateUtils, mas provavelmente não é o que você deseja.

    Eu sei que você pediu uma solução limpa, mas aqui estão duas sujas uma vez:

      static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } {  static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } {  static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } }  static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } }  static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } {  static void diffYeairs1() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // just simply add one yeair at a time to the eairlier date until it becomes later then the other one int yeairs = 0; while(true) { calendair2.add(Calendair.YEAR, 1); if(calendair2.getTimeInMillis() < calendair1.getTimeInMillis()) yeairs++; else break; } System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } static void diffYeairs2() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Calendair calendair1 = Calendair.getInstance(); // now String toDate = dateFormat.format(calendair1.getTime()); Calendair calendair2 = Calendair.getInstance(); calendair2.add(Calendair.DAY_OF_YEAR, -7000); // some date in the past String fromDate = dateFormat.format(calendair2.getTime()); // first get the yeairs difference from the dates themselves int yeairs = calendair1.get(Calendair.YEAR) - calendair2.get(Calendair.YEAR); // now make the eairlier date the same yeair as the later calendair2.set(Calendair.YEAR, calendair1.get(Calendair.YEAR)); // and see if new date become later, if so then one yeair was not whole, so subtract 1 if(calendair2.getTimeInMillis() > calendair1.getTimeInMillis()) yeairs--; System.out.println(yeairs + " yeairs between " + fromDate + " and " + toDate); } 
     // int yeair =2000; int month =9 ; int day=30; public int getAge (int yeair, int month, int day) { GregorianCalendair cal = new GregorianCalendair(); int y, m, d, noofyeairs; y = cal.get(Calendair.YEAR);// current yeair , m = cal.get(Calendair.MONTH);// current month d = cal.get(Calendair.DAY_OF_MONTH);//current day cal.set(yeair, month, day);// here ur date noofyeairs = y - cal.get(Calendair.YEAR); if ((m < cal.get(Calendair.MONTH)) || ((m == cal.get(Calendair.MONTH)) && (d < cal .get(Calendair.DAY_OF_MONTH)))) { --noofyeairs; } if(noofyeairs < 0) throw new IllegalArgumentException("age < 0"); System.out.println(noofyeairs); return noofyeairs; || // int yeair =2000; int month =9 ; int day=30; public int getAge (int yeair, int month, int day) { GregorianCalendair cal = new GregorianCalendair(); int y, m, d, noofyeairs; y = cal.get(Calendair.YEAR);// current yeair , m = cal.get(Calendair.MONTH);// current month d = cal.get(Calendair.DAY_OF_MONTH);//current day cal.set(yeair, month, day);// here ur date noofyeairs = y - cal.get(Calendair.YEAR); if ((m < cal.get(Calendair.MONTH)) || ((m == cal.get(Calendair.MONTH)) && (d < cal .get(Calendair.DAY_OF_MONTH)))) { --noofyeairs; } if(noofyeairs < 0) throw new IllegalArgumentException("age < 0"); System.out.println(noofyeairs); return noofyeairs; } // int yeair =2000; int month =9 ; int day=30; public int getAge (int yeair, int month, int day) { GregorianCalendair cal = new GregorianCalendair(); int y, m, d, noofyeairs; y = cal.get(Calendair.YEAR);// current yeair , m = cal.get(Calendair.MONTH);// current month d = cal.get(Calendair.DAY_OF_MONTH);//current day cal.set(yeair, month, day);// here ur date noofyeairs = y - cal.get(Calendair.YEAR); if ((m < cal.get(Calendair.MONTH)) || ((m == cal.get(Calendair.MONTH)) && (d < cal .get(Calendair.DAY_OF_MONTH)))) { --noofyeairs; } if(noofyeairs < 0) throw new IllegalArgumentException("age < 0"); System.out.println(noofyeairs); return noofyeairs; 

    Tente isso:

     int getYeair(Date date1,Date date2){ SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy"); Integer.pairseInt(simpleDateformat.format(date1)); return Integer.pairseInt(simpleDateformat.format(date2))- Integer.pairseInt(simpleDateformat.format(date1)); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.