java判断系统时间是否在规定的时间区间之内
java判断系统时间是否在规定的时间区间之内
1️⃣ 创建controller => service => serviceimpl
package com.preciouslove.xinxin_emo.controller; import com.preciouslove.xinxin_emo.service.UserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Controller; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * @Author : YuanXin * @create 2023/2/15 15:29 * @Description : */ @Controller @EnableScheduling // 开启定时 public class timeBetween { @Autowired private UserInfoService userInfoService; @Scheduled(cron = "1 * * * * * ") public void Between() throws ParseException { String nowDate = new SimpleDateFormat("HH:mm:ss").format(new Date()); Date date = new SimpleDateFormat("HH:mm:ss").parse(nowDate); Date begin = new SimpleDateFormat("HH:mm:ss").parse("14:00:00"); Date end = new SimpleDateFormat("HH:mm:ss").parse("17:00:00"); long endTime = end.getTime(); long beginTime = begin.getTime(); long dateTime = date.getTime(); boolean result = userInfoService.betweenDate(dateTime,beginTime,endTime); if (result){ System.out.println("在时间范围之内"); } else { System.out.println("不在时间范围之内"); } } }
✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨