如何将cron表达式转换为日期或时间 [英] How to convert cron expression to date or time

查看:3204
本文介绍了如何将cron表达式转换为日期或时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将cron表达式转换为date。
例如这是 0 0 23 1 1-12?表达式。我想转换为相应的日期。
我如何实现这一点?



这是我试过,但它只是给了描述,人类可读,但我想实现适当的日期格式。 p>

  import java.text.ParseException; import java.util.Date; import org.quartz.CronExpression; public class CRON {public static void main args [])throws ParseException {// AutoRegistrationJobDao obj = new AutoRegistrationJobDao(); //obj.updateLastAutoRegSchdl(); String cron =0 0 23 1 1-12?; System.out.println(cron:+ cron); CronExpression cronExpression = new CronExpression(0 0 23 1 1-12?); System.out.println(cronExpression.getExpressionSummary()); System.out.println(cronExpression.getFinalFireTime()); //System.out.println(cronExpression.getNextValidTimeAfter(date)); try {CronExpression c = new CronExpression(cron);日期date = c.getFinalFireTime(); System.out.println(date); System.out.println(c.getFinalFireTime()); } catch(ParseException e){// TODO自动生成的catch块e.printStackTrace(); }这个是我试过,我使用多个方法和使用不同的类,但没有运气$ b $ {
} $ b输出:

 秒:0 
分钟:0
小时:23
daysOfMonth:1
months:1,2,3,4,5,6,7,8,9,10,11,12
daysOfWeek:?
lastdayOfWeek:false
nearestWeekday:false
NthDayOfWeek:0
lastdayOfMonth:false


解决方案

* / 20 * * * * command - >这将在20分钟内执行一次。这不能映射到一个单一的时间点。



给定一个cron表达式 - 将有多个实例在作业应该被触发的时间。所以你不能期望一个一对一的映射b / w cron和日期实例。



如果你有一个日期引用,你可以计算出下一个作业触发时间或从参考日期等。


I am trying to convert the cron expression to date. for example this is 0 0 23 1 1-12 ? expression. I want to convert to corresponding date. How do I achieve this?

This is what I tried but it just give the description, Human readable but I want to achieve proper date format.

import java.text.ParseException;
import java.util.Date;

import org.quartz.CronExpression;

public class CRON {
	
	public static void main(String args[]) throws ParseException
	{
		//AutoRegistrationJobDao obj=new AutoRegistrationJobDao();
		//obj.updateLastAutoRegSchdl();
		
		String cron ="0 0 23 1 1-12 ?";
		System.out.println("cron :"+cron);
		
		CronExpression cronExpression =new CronExpression("0 0 23 1 1-12 ?");
		System.out.println(cronExpression.getExpressionSummary());
		System.out.println(cronExpression.getFinalFireTime());
		//System.out.println(cronExpression.getNextValidTimeAfter(date));
		
		try {
			CronExpression c =new CronExpression(cron);
			Date date=c.getFinalFireTime();
			System.out.println(date);
			
			
			System.out.println(c.getFinalFireTime());
			
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
   
}

This is what I tried, I am using multiple method and using different class, but no luck Output:

seconds: 0
minutes: 0
hours: 23
daysOfMonth: 1
months: 1,2,3,4,5,6,7,8,9,10,11,12
daysOfWeek: ?
lastdayOfWeek: false
nearestWeekday: false
NthDayOfWeek: 0
lastdayOfMonth: false

解决方案

*/20 * * * * command -> this will execute once in 20 min. This cannot be mapped to a single point of time.

Given a cron expression - there will be multplie instance in time when the job should get triggered. So you cannot expect a one to one mapping b/w the cron and a date instance.

If you have a date reference, you can figure out the next job trigger time or the like from the reference date.

这篇关于如何将cron表达式转换为日期或时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆