软件编程
位置:首页>> 软件编程>> Android编程>> Android 判断日期是否在一年以内的算法实例

Android 判断日期是否在一年以内的算法实例

作者:jingxian  发布时间:2023-03-22 06:04:13 

标签:判断,日期,一年以内,android

项目中需要判断传入的日期是否在未来的一年以内,百度了一下网上没有找到好的方式,写了,方便自己和他人:


int datecompareAfter = compareDate(new Date(), date);
int daecompareBefore = compareDate(date, getOneYear());

if (datecompareAfter == -1 && daecompareBefore == -1) {

//如果不是在一年以内,则弹出提示

} else {
//在一年以内做的逻辑
}

// 比较时间
public int compareDate(Date d1, Date d2) {
if (d1.getTime() > d2.getTime()) {
return 1;
} else if (d1.getTime() < d2.getTime()) {
return -1;
} else {// 相等
return 0;
}
}

//當前時間加1年
public Date getOneYear() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, 1);
return c.getTime();
}
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com