public class test{
/**
* 将 Date格式转为字符串时间格式
* @param date 日期
* @param format 转换的格式 例:yyyy-MM-dd
* @return
*/
public static String dateByStringTime(Date date, String format) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 判断日期是否是昨天
* @param oneTime 日期
* @return
*/
public static boolean compareOneAndYesterday(String oneTime) {
if (oneTime == null) {
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_MONTH, -1);
String nowTime = sdf.format(c.getTime());
return nowTime.equals(oneTime);
}
//测试
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yesterday = sdf.parse("2020-11-20 18:28:30");
boolean b = compareOneAndYesterday(dateByStringTime(yesterday, "yyyy-MM-dd"));
if (b) {
System.out.println("是昨天");
} else {
System.out.println("不是昨天");
}
}
}
判断日期是否是昨天
最新推荐文章于 2023-07-06 13:51:38 发布