我正在尝试确定某个日期的年龄。有没有人知道在Android中这样做的干净方法?我显然有Java api可用,但直接的java api非常弱,我希望Android能帮到我。
编辑:在Android中使用Joda时间的多个建议让我担心Android Java - Joda Date is slow及相关问题。此外,拉入一个未随平台一起提供的库,这个尺寸可能太大了。
答案 0 :(得分:96)
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;
}
答案 1 :(得分:15)
我建议使用优秀的Joda-Time库来处理与Java相关的所有日期。
根据您的需要,您可以使用Years.yearsBetween()
方法。
答案 2 :(得分:10)
ChronoUnit.YEARS.between(
LocalDate.of( 2010 , 1 , 1 ) ,
LocalDate.now( ZoneId.of( "America/Montreal" ) )
)
旧的日期时间课程真的很糟糕,太糟糕了,Sun& Oracle同意用java.time类取代它们。如果您使用日期时间值进行任何重要工作,那么在项目中添加库是值得的。 Joda-Time库非常成功并推荐,但现在处于维护模式。该团队建议迁移到java.time类。
大部分java.time功能都被反向移植到Java 6& ThreeTen-Backport中的7,并进一步适应Android中的ThreeTenABP(见How to use…)。
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
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.Date
,Calendar
和& SimpleDateFormat
现在位于Joda-Time的maintenance mode项目建议迁移到java.time类。
要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*
类。
从哪里获取java.time类?
ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如Interval
,YearWeek
,YearQuarter
和more。
答案 3 :(得分:3)
我显然无法发表评论,但我认为你可以使用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的数量。只需保留所有内容,这应该是大部分时间(闰年,哎哟)应该足够好,但这取决于你的应用程序的年数和功能必须是多么高效的天气,这将是值得的那种黑客。
答案 4 :(得分:1)
我知道你要求一个干净的解决方案,但这里有两个脏的一次:
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);
}
答案 5 :(得分:1)
以下是我认为更好的方法:
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);
}
修改强>
除了我修复的错误之外,这种方法在闰年时效果不佳。这是一个完整的测试套件。我猜你最好使用接受的答案。
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);
}
}
}
答案 6 :(得分:0)
如果您不想使用java的日历计算它,您可以使用Androids Time class它应该更快但我在切换时没有注意到太多差异。
我找不到任何预定义的函数来确定Android中某个年龄的2个日期之间的时间。有一些很好的辅助函数可以在DateUtils中的日期之间获得格式化时间,但这可能不是你想要的。
答案 7 :(得分:0)
试试这个:
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));
}
答案 8 :(得分:0)
// 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;
答案 9 :(得分:0)
这将起作用,并且如果您希望年数将12替换为1
String date1 = "07-01-2015";
String date2 = "07-11-2015";
int i = Integer.parseInt(date1.substring(6));
int j = Integer.parseInt(date2.substring(6));
int p = Integer.parseInt(date1.substring(3,5));
int q = Integer.parseInt(date2.substring(3,5));
int z;
if(q>=p){
z=q-p + (j-i)*12;
}else{
z=p-q + (j-i)*12;
}
System.out.println("The Total Months difference between two dates is --> "+z+" Months");
答案 10 :(得分:0)
感谢@Ole V.v对其进行了审查:我发现一些内置库类具有相同的作用
int noOfMonths = 0;
org.joda.time.format.DateTimeFormatter formatter = DateTimeFormat
.forPattern("yyyy-MM-dd");
DateTime dt = formatter.parseDateTime(startDate);
DateTime endDate11 = new DateTime();
Months m = Months.monthsBetween(dt, endDate11);
noOfMonths = m.getMonths();
System.out.println(noOfMonths);
答案 11 :(得分:0)
您只需计算两个日期毫秒之间的差,然后除以秒,分钟,小时,天和月即可。
尝试一下
public int findDiff(Date fromDate, Date toDate) {
if(fromDate == null || toDate == null) {
return -1;
}
long diff = toDate.getTime() - fromDate.getTime();
int diffInYears = (int) (diff / (60 * 60 * 1000 * 24 * 30.41666666 * 12));
return diffInYears;
}
答案 12 :(得分:0)
如果您不想设置日历、区域设置或外部库的边界,这是一个方便的工具:
private static SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
public static Integer toDate8d(Date date) {
String s;
synchronized (YYYYMMDD) { s = YYYYMMDD.format(date); } // SimpleDateFormat thread safety
return Integer.valueOf(s);
}
public static Integer yearDiff(Date pEarlier, Date pLater) {
return (toDate8d(pLater) - toDate8d(pEarlier)) / 10000;
}