惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - 一叶知秋。

安装nginx和OpenSSL struts2的<s:if test>标签的注意点 mysql怎么写逻辑比较清晰? 设置idea 代码报错时不弹框显示 Collections.sort多个字段排序 LigerUI 中的 Grid (ligerGrid) 合并单元格 mysql模糊查找数据库使用的字段名 spring RestTemplate忽略证书验证 关于easyExcel解析未添加@ExcelProperty报错问题分析 Nginx的常用命令(启动重启停止等) 解决远程调用三方接口:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException报错 msyql查询表的索引sql语句 MySQL查看表占用空间大小 js屏蔽回车提交 kettle同步表数据null处理 MySql 分组后获取距离时间最近的第一条数据 mysql解表和查看锁表 Windows关闭指定端口命令 Windows 技术篇-防火墙启用时指定外部可访问端口,防火墙开放端口设置
从包含时间属性的对象列表中,筛选出时间≤指定时间参数且最接近该时间参数的那个对象
一叶知秋。 · 2026-01-30 · via 博客园 - 一叶知秋。

1. 第一步:定义包含 Date 时间属性的实体类

 
import java.util.Date;

/**
 * 包含Date类型时间属性的业务对象
 */
public class TimeObject {
    // 自定义业务字段(示例)
    private Long id;
    private String name;
    // 核心时间属性(Date类型)
    private Date time;

    // 构造方法
    public TimeObject(Long id, String name, Date time) {
        this.id = id;
        this.name = name;
        this.time = time;
    }

    // Getter/Setter(必须,用于获取/设置时间属性)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    // 重写toString,方便测试输出
    @Override
    public String toString() {
        return "TimeObject{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", time=" + time +
                '}';
    }
}

2. 第二步:核心工具方法(非 Stream 实现)

核心逻辑通过基础 for 循环+手动比较时间差实现,步骤:

  1. 空值校验(避免空指针);
  2. 遍历列表,筛选出「时间≤指定时间」的对象;
  3. 在符合条件的对象中,找到与指定时间时间差最小的那个(即最接近)。
import java.util.Date;
import java.util.List;

/**
 * 时间匹配工具类(非Stream实现,兼容所有Java版本)
 */
public class TimeMatchUtil {

    /**
     * 从List<TimeObject>中筛选:时间≤指定时间,且最接近该时间的对象
     * @param objectList 待筛选的对象列表
     * @param targetTime 指定的时间参数(Date类型)
     * @return 符合条件的对象(无则返回null)
     */
    public static TimeObject findClosestNotExceedTime(List<TimeObject> objectList, Date targetTime) {
        // 1. 空值校验(工业级代码必备)
        if (objectList == null || objectList.isEmpty() || targetTime == null) {
            return null;
        }

        // 定义变量:存储最终匹配的对象、最小时间差(毫秒数)
        TimeObject closestObj = null;
        long minTimeDiff = Long.MAX_VALUE; // 初始化为最大长整型,确保第一次比较会替换

        // 2. 遍历列表,逐个比较
        for (TimeObject obj : objectList) {
            // 跳过对象时间为null的情况
            if (obj.getTime() == null) {
                continue;
            }

            Date objTime = obj.getTime();
            // 3. 过滤条件:对象时间 ≤ 指定时间(Date的compareTo方法:返回值≤0 表示≤)
            if (objTime.compareTo(targetTime) > 0) {
                continue; // 时间超过目标值,直接跳过
            }

            // 4. 计算当前对象与目标时间的时间差(毫秒数,目标时间 - 对象时间)
            long timeDiff = targetTime.getTime() - objTime.getTime();
            // 确保时间差为非负数(理论上不会出现,因已过滤≤的情况)
            timeDiff = Math.abs(timeDiff);

            // 5. 找到时间差更小的对象,更新结果
            if (timeDiff < minTimeDiff) {
                minTimeDiff = timeDiff;
                closestObj = obj;
            }
        }

        // 6. 返回最终匹配的对象(无符合条件则为null)
        return closestObj;
    }
}