我怎样才能find在Android中的两个date之间的年数?
我正在试图确定从某个特定date开始的年龄。 有谁知道一个干净的方式来做到这一点在Android? 我显然可以使用Java API,但直接使用的Java API非常薄弱,我希望Android能够帮助我。
编辑:在Android中使用Joda时间的多个build议令我有点担心,由于Android Java – 乔达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; }
我build议使用伟大的Joda-Time库来处理与Java相关的所有date。
为了您的需要,您可以使用Years.yearsBetween()
方法。
TL;博士
ChronoUnit.YEARS.between( LocalDate.of( 2010 , 1 , 1 ) , LocalDate.now( ZoneId.of( "America/Montreal" ) ) )
java.time
旧的date – 时间类真的很糟糕,太糟糕了,Sun和Oracle都同意用java.time类来代替它们。 如果您根据date时间值做任何重要的工作,那么将库添加到您的项目中是值得的。 乔达时间图书馆非常成功,并build议,但现在在维护模式。 团队build议迁移到java.time类。
大部分的java.timefunction在ThreeTen-Backport中移植到Java 6&7中,并进一步适用于ThreeTenABP中的 Android (请参阅如何使用… )。
LocalDate start = LocalDate.of( 2010 , 1 , 1 ) ; LocalDate stop = LocalDate.now( ZoneId.of( "America/Montreal" ) ); long years = java.time.temporal.ChronoUnit.YEARS.between( start , stop );
转储到控制台。
System.out.println( "start: " + start + " | stop: " + stop + " | years: " + years ) ;
开始:2010-01-01 | 停止:2016-09-06 | 年:6
我显然还不能评论,但我认为你可以使用DAY_OF_YEAR锻炼,如果你应该调整一年(从当前最好的答案复制和修改)
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; }
同样的,你也许只需要对时间的ms表示进行比较,然后除以一年中的ms数。 只要把所有东西都放在很长的时间里,大多数情况下都应该足够好(闰年,哎哟),但是这取决于你的应用程序的年数,以及这个函数的性能如何,这是值得的。
这是我认为是一个更好的方法:
public int getYearsBetweenDates(Date first, Date second) { Calendar firstCal = GregorianCalendar.getInstance(); Calendar secondCal = GregorianCalendar.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendar.DAY_OF_YEAR, 1 - firstCal.get(Calendar.DAY_OF_YEAR)); return secondCal.get(Calendar.YEAR) - firstCal.get(Calendar.YEAR); }
编辑
除了我修正的错误之外,这个方法在闰年里效果不好。 这是一个完整的testing套件。 我想你最好用接受的答案。
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; class YearsBetweenDates { public static int getYearsBetweenDates(Date first, Date second) { Calendar firstCal = GregorianCalendar.getInstance(); Calendar secondCal = GregorianCalendar.getInstance(); firstCal.setTime(first); secondCal.setTime(second); secondCal.add(Calendar.DAY_OF_YEAR, 1 - firstCal.get(Calendar.DAY_OF_YEAR)); return secondCal.get(Calendar.YEAR) - firstCal.get(Calendar.YEAR); } private static class TestCase { public Calendar date1; public Calendar date2; public int expectedYearDiff; public String comment; public TestCase(Calendar date1, Calendar date2, int expectedYearDiff, String comment) { this.date1 = date1; this.date2 = date2; this.expectedYearDiff = expectedYearDiff; this.comment = comment; } } private static TestCase[] tests = { new TestCase( new GregorianCalendar(2014, Calendar.JULY, 15), new GregorianCalendar(2015, Calendar.JULY, 15), 1, "exactly one year"), new TestCase( new GregorianCalendar(2014, Calendar.JULY, 15), new GregorianCalendar(2017, Calendar.JULY, 14), 2, "one day less than 3 years"), new TestCase( new GregorianCalendar(2015, Calendar.NOVEMBER, 3), new GregorianCalendar(2017, Calendar.MAY, 3), 1, "a year and a half"), new TestCase( new GregorianCalendar(2016, Calendar.JULY, 15), new GregorianCalendar(2017, Calendar.JULY, 15), 1, "leap years do not compare correctly"), }; public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); for (TestCase t : tests) { int diff = getYearsBetweenDates(t.date1.getTime(), t.date2.getTime()); String result = diff == t.expectedYearDiff ? "PASS" : "FAIL"; System.out.println(t.comment + ": " + df.format(t.date1.getTime()) + " -> " + df.format(t.date2.getTime()) + " = " + diff + ": " + result); } } }
如果你不想使用Java的日历来计算它,你可以使用Android的时间类它应该是更快,但我没有注意到,当我切换没有太大的差异。
我无法find任何预定义的函数来确定Android中一个年龄的两个date之间的时间。 有一些很好的帮助函数可以在DateUtils的date之间获得格式化的时间,但这可能不是你想要的。
我知道你已经要求一个干净的解决scheme,但这里有两个肮脏的一次:
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); }
// int year =2000; int month =9 ; int day=30; public int getAge (int year, int month, int day) { GregorianCalendar cal = new GregorianCalendar(); int y, m, d, noofyears; y = cal.get(Calendar.YEAR);// current year , m = cal.get(Calendar.MONTH);// current month d = cal.get(Calendar.DAY_OF_MONTH);//current day cal.set(year, month, day);// here ur date noofyears = y - cal.get(Calendar.YEAR); if ((m < cal.get(Calendar.MONTH)) || ((m == cal.get(Calendar.MONTH)) && (d < cal .get(Calendar.DAY_OF_MONTH)))) { --noofyears; } if(noofyears < 0) throw new IllegalArgumentException("age < 0"); System.out.println(noofyears); return noofyears;
尝试这个:
int getYear(Date date1,Date date2){ SimpleDateFormat simpleDateformat=new SimpleDateFormat("yyyy"); Integer.parseInt(simpleDateformat.format(date1)); return Integer.parseInt(simpleDateformat.format(date2))- Integer.parseInt(simpleDateformat.format(date1)); }