java 判断是否是昨天
/**
* 判断是否是昨天
*
* @param date 判断该日期是否是昨天
* @return 是 true 不是 false
*/
public static boolean isYesterday(Date date) {
boolean flag = false;
// 先获取年份
int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
// 获取当前年份 和 一年中的第几天
int day = getDayNumForYear(date);
int currentYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
int currentDay = getDayNumForYear(new Date());
// 计算 如果是去年的
if (currentYear - year == 1) {
// 如果当前正好是 1月1日 计算去年有多少天,指定时间是否是一年中的最后一天
if (currentDay == 1) {
int yearDay;
if (year % 400 == 0) {
// 世纪闰年
yearDay = 366;
} else if (year % 4 == 0 && year % 100 != 0) {
// 普通闰年
yearDay = 366;
} else {
// 平年
yearDay = 365;
}
if (day == yearDay) {
flag = true;
}
}
} else {
if (currentDay - day == 1) {
flag = true;
}
}
return flag;
}
public static Integer getDayNumForYear(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
return ca.get(Calendar.DAY_OF_YEAR);
}