当前位置:   article > 正文

java 日期判断 给定日期是否为当天 一周7天以内 一周7天以外_判断时间是否是7天内 java

判断时间是否是7天内 java
public static boolean isToday(String validateDate){
final String format = "yyyy-MM-dd HH:mm:ss";
Date vDate  = null;
try {
vDate = str2Date(validateDate,format);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
Date today = new Date();
today.setHours(23);
today.setMinutes(59);
today.setSeconds(59);
long diff = today.getTime()-vDate.getTime();
if(diff<0){
return false;
}else{
long days = diff/(1000*60*60*24); 
if(days==0){
return true;
}else{
return false;
}
}

}


/**
*距离当前时间七天之内的日期,和七天之外的日期
* @param dt
* @param type 
* type:1--7天之内的 
* type: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==1 && days>0 && days<=7)return true;
if(type==2 && days>7)return true;


return false;


/**
* 将string 按指定格式转化为java.util.Date

* @param str
* @param format
* @return
* @throws ParseException
*/
public static Date str2Date(String str, String format)
throws ParseException {
if (str == null || "".equals(str)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return (Date) sdf.parse(str);
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/260616?site
推荐阅读
相关标签
  

闽ICP备14008679号