直接上代码,不做过多解释了
package testJava; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class IsEffectiveDate { public static void main(String[] args) throws ParseException { String format = "yy-mm-dd HH:mm:ss"; //时间格式这里可以自己随便改 //下面这种就可以查看到毫秒 System.out.println("当前时间:" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS") .format(new Date() )); Date nowTime = new SimpleDateFormat(format).parse("2020-11-13 15:28:00"); Date startTime = new SimpleDateFormat(format).parse("2020-11-13 15:27:00"); Date endTime = new SimpleDateFormat(format).parse("2020-11-13 15:27:59"); System.out.println(isEffectiveDate(nowTime, startTime, endTime)); } /** *判断当前时间是否在时间区间内 */ private static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) { if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) { return true; } return nowTime.after(startTime) && nowTime.before(endTime); } }
运行结果: