赞
踩
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class VerifyUtils {
public static void main(String[] args) throws ParseException {
verifyEffectiveTime("20230521155000", 5);
}
/**
* 校验时间有效期
* @param dateTime 时间yyyyMMddHHmmss
* @param effectiveTime 有效时间分钟(整数)
* @return true有效时间内,false有效时间外
* @throws ParseException
*/
public static boolean verifyEffectiveTime(String dateTime, int effectiveTime) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = format.parse(dateTime);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
Calendar c3 = Calendar.getInstance();
c1.setTime(date);//要判断的日期
c2.setTime(new Date());//初始日期
c3.setTime(new Date());//也给初始日期 把分钟加五
c3.add(Calendar.MINUTE, effectiveTime);
c2.add(Calendar.MINUTE, effectiveTime * -1);//减去五分钟
System.out.println("c1==" + c1.getTime());
System.out.println("c2==" + c2.getTime());
System.out.println("c3==" + c3.getTime());
if (c1.after(c2) && c1.before(c3)) {
System.out.println("result==" + true);
return true;
}
System.out.println("result==" + false);
return false;
}
}
打印结果:
c1==Sun May 21 15:50:00 CST 2023
c2==Mon May 22 15:46:26 CST 2023
c3==Mon May 22 15:56:26 CST 2023
result==false
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。