java如何获取整分,整时的时间戳

java获取时间戳的方式是System.currentTimeMillis(),但是返回的是毫秒级时间戳

需求是啥?

获取整5分钟的时间(也就是5分,10分,15分……这些时间点),发送一次监控信息。

实现

System.currentTimeMillis()获取到毫秒级别时间戳,通过获取当前时间除以你想要的时间间隔跨度(我需要的是5分钟)。实现的函数如下

    private static long nextMonitorTime() {
        // 获取每个5分钟的整分时间戳
        long now = System.currentTimeMillis();
        return now - now % (1000 * 60 * 5) + (1000 * 60 * 5);
    }

总结

毕业前说打死都不写java,结果真香 》**《

Life is more than the present.