ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

判断当前时间是否在给定的时间区间内

2026/9/21 12:18:16 拓冰建站 浏览量
判断当前时间是否在给定的时间区间内

直接看代码

package com.lsm.practice.date;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** @author Eme* @since 2024-01-05**/public class DateUtils {/*** 通过指定的Date获取新的Date* 如果pattern只包含时间段,则返回的Date中年月日是默认值,* 示例:pattern=HHmmss, 返回的Date为Thu Jan 01 23:59:59 CST 1970,其中22:42:26为指定的Date中的小时分钟秒数** @param date* @param pattern* @return* @throws ParseException*/public static Date getDate(String date, String pattern) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat(pattern);return sdf.parse(date);}/*** 通过指定的Date获取新的Date* 如果pattern只包含时间段,则返回的Date中年月日是默认值,* 示例:pattern=HHmmss, 返回的Date为Thu Jan 01 23:59:59 CST 1970,其中22:42:26为指定的Date中的小时分钟秒数** @param date* @param pattern* @return* @throws ParseException*/public static Date getDate(Date date, String pattern) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat(pattern);return sdf.parse(sdf.format(date));}public static void main(String[] args) {try {//isValidTime();isValidDate();} catch (ParseException e) {e.printStackTrace();}}public static void isValidDate() throws ParseException {String startDateStr = "20240101";String endDateStr = "20240115";String pattern = "yyyyMMdd";Date startDate = getDate(startDateStr, pattern);System.out.println("startDate=" + startDate);Date endDate = getDate(endDateStr, pattern);System.out.println("endDate=" + endDate);//Date nowDate = getDate(new Date(), pattern);Date nowDate = getDate("20240101", pattern);System.out.println("nowDate=" + nowDate);if (nowDate.after(startDate) && nowDate.before(endDate)) {System.out.println("after和before,在目标时间区间内");} else {System.out.println("after和before,不在目标时间区间内");}if (nowDate.compareTo(startDate) >= 0 && nowDate.compareTo(endDate) <= 0) {System.out.println("compareTo,在目标时间区间内");} else {System.out.println("compareTo,不在目标时间区间内");}}/*** 通过给定时间区间判断指定的时间是否是有效时间*/public static void isValidTime() throws ParseException {String startTime = "000000";String endTime = "235959"; // 可以通过调大endTime,让其大于235959,比如250000,则得到的Date就是1970年1月2日的1点String pattern = "HHmmss";Date startDate = getDate(startTime, pattern);Date nowDate = getDate(new Date(), pattern);System.out.println("nowDate=" + nowDate);Date endDate = getDate(endTime, pattern);System.out.println("endDate=" + endDate);/** after和before是不包含边界的,即(a,b)。* 示例:当前时间为090000(格式HHmmss),开始时间为090000,结束时间为160000* 用after和before判断是否在区间内,得出是不在区间内的*/if (nowDate.after(startDate) && nowDate.before(endDate)) {System.out.println("after和before,在目标时间区间内");} else {System.out.println("after和before,不在目标时间区间内");}// compareTo可以通过=符号使包含边界,即[a,b]if (nowDate.compareTo(startDate) >= 0 && nowDate.compareTo(endDate) <= 0) {System.out.println("compareTo,在目标时间区间内");} else {System.out.println("compareTo,不在目标时间区间内");}}public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {if (nowTime.getTime() == startTime.getTime()|| nowTime.getTime() == endTime.getTime()) {return true;}Calendar date = Calendar.getInstance();date.setTime(nowTime);Calendar begin = Calendar.getInstance();begin.setTime(startTime);Calendar end = Calendar.getInstance();end.setTime(endTime);return date.after(begin) && date.before(end);}}