`
giga_Zhang
  • 浏览: 153547 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

判断给定日期是否为当天

阅读更多
此函数可以用于实现对按时间排序的记录进行归类,比如:可以区分当天的记录,一周之内的记录和更早的记录。现在好多邮件系统都实现了此功能。

/**
  * 判断给定日期是否为当天,

  *距离当前时间七天之内的日期,和七天之外的日期

  * @param dt
  * @param type  0--当天  1--7天之内的 2--7天之外的
  * @return
  */
public static boolean getDayDiffFromToday(Date dt,int type){
  Date today=new Date();
  today.setHours(23);
  today.setMinutes(59);
  today.setSeconds(59);
 
  long diff = today.getTime() - dt.getTime();
  if(diff<0)diff=0;
  long days = diff/(1000*60*60*24);
 
  if(type==0 && days==0)return true;
  if(type==1 && days>0 && days<=7)return true;
  if(type==2 && days>7)return true;
 
  return false;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics