【Java/DateTime】用当前日期时间与确定日期时间比较,大于则执行某动作
代码:
package logbackCfg; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class NewDayThread { private final static Logger logger = LoggerFactory.getLogger(NewDayThread.class); public static void main(String[] args) throws InterruptedException { while(true) { Date currentTime = new Date(); if(currentTime.compareTo(fixedDate())>0) { Thread.sleep(8000); logger.info("万里赴戎机,关山度若飞。"); logger.error("朔气传金柝,寒光照铁衣。"); logger.debug("将军百战死,壮士十年归。"); } } } private static Date fixedDate() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return formatter.parse("2020-01-16 10:39:00"); } catch (ParseException e) { return new Date(); } } }
上面代码就是那当前时间与2020-01-16 10:39:00 比较,如果大于则写log。
以下就是线程在设定时间写log的效果:
... 2020-01-16 23:59:40.808 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-16 23:59:42.527 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-16 23:59:42.527 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-16 23:59:42.527 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-16 23:59:42.527 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-16 23:59:45.809 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-16 23:59:45.809 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-16 23:59:45.809 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-16 23:59:45.809 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-16 23:59:50.528 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-16 23:59:50.528 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-16 23:59:50.528 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-16 23:59:50.528 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-16 23:59:50.809 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-16 23:59:50.809 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-16 23:59:50.809 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-16 23:59:50.809 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-16 23:59:55.810 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-16 23:59:55.810 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-16 23:59:55.810 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-16 23:59:55.810 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-16 23:59:58.529 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-16 23:59:58.529 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-16 23:59:58.529 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-16 23:59:58.529 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:00.810 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:00.810 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:00.810 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:00.810 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:05.811 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:05.811 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:05.811 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:05.811 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:06.530 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:06.530 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:06.530 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:06.530 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:08.004 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:08.007 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:08.007 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:10.811 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:10.811 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:10.811 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:10.811 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:14.530 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:14.530 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:14.530 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:14.530 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:15.812 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:15.812 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:15.812 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:15.812 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:16.008 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:16.008 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:16.008 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:20.812 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:20.812 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:20.812 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:20.812 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:22.530 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:22.530 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:22.530 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:22.530 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:24.008 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:24.008 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:24.008 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:25.812 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:25.812 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:25.812 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:25.812 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:30.531 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:30.531 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:30.531 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:30.531 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:30.812 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:30.812 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:30.812 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:30.812 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:32.009 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:32.009 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:32.009 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:35.812 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:35.812 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:35.812 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:35.812 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:38.532 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:38.532 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:38.532 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:38.532 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:40.010 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:40.010 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:40.010 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:40.813 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:40.813 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:40.813 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:40.813 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:45.813 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:45.813 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:45.813 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:45.813 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:46.532 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:46.532 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:46.532 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:46.532 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王 2020-01-17 00:00:48.010 [main] INFO logbackCfg.NewDayThread -万里赴戎机,关山度若飞。 2020-01-17 00:00:48.010 [main] ERROR logbackCfg.NewDayThread -朔气传金柝,寒光照铁衣。 2020-01-17 00:00:48.010 [main] DEBUG logbackCfg.NewDayThread -将军百战死,壮士十年归。 2020-01-17 00:00:50.814 [main] INFO logbackCfg.App -秦时明月汉时关 2020-01-17 00:00:50.814 [main] ERROR logbackCfg.App -万里长征人未还 2020-01-17 00:00:50.814 [main] DEBUG logbackCfg.App -但使龙城飞将在 2020-01-17 00:00:50.814 [main] TRACE logbackCfg.App -不教胡马度阴山 2020-01-17 00:00:54.533 [main] INFO logbackCfg.App2 -九里山前作战场 2020-01-17 00:00:54.533 [main] ERROR logbackCfg.App2 -牧童拾得旧刀枪 2020-01-17 00:00:54.533 [main] DEBUG logbackCfg.App2 -微风吹皱乌江水 2020-01-17 00:00:54.533 [main] TRACE logbackCfg.App2 -恰似虞姬别霸王
--END-- 2020-01-16 10:48
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)