AD命令AD命令09-11 22:47

判断时间点处于今天

hutool简介:https://blog.csdn.net/RYAN0UP/article/details/80772693

用到了hutool,下面是maven依赖

<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>4.3.2</version>
</dependency>

代码:

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Test {
    public static void main(String[] args) {
        String time = "2019-04-24 00:00:00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(time, dtf);

        boolean boo = jud(localTime);
        System.out.println(boo);

    }
    //判断时间点是否处于今天
    public static boolean jud(LocalDateTime localTime){

        //得到今天的开始,也就是今天的00:00:00
        DateTime startTime = DateUtil.beginOfDay(new Date());
        //得到今天的结尾,也就是今天的23:59:59
        DateTime endTime = DateUtil.endOfDay(new Date());

        //isIn(我们要判断的时间点,开始时间, 结束时间)
        //是个闭区间,也就是时间点等于开始时间和结束时间,都算在这个时间段范围内
        boolean boo = DateUtil.isIn(locaDateTimeToUdate(localTime),startTime,endTime);

        return boo;
    }
    
    //LocalDateTime转换为Date类型
    public static Date locaDateTimeToUdate(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return Date.from(instant);
    }
}

程序之家二维码

000
评论