开发者

Create A cron Expression for Weekly

开发者 https://www.devze.com 2023-02-20 11:22 出处:网络
Suppose I pass the value like 2009-07-13T18:00:开发者_如何学C00 I have to create a cron expression using Java to get like* * 18 13 07 2009 ?

Suppose I pass the value like 2009-07-13T18:00:开发者_如何学C00

I have to create a cron expression using Java to get like * * 18 13 07 2009 ?

Can you please help me in this?


Your crontab(5) expression:

* * 18 13 07 2009

will execute every minute of every hour on the 18th day of the 13th month, on the 2009th day in the week. :)

Jul 13, 2009, was a Monday. A crontab entry that executes every week, with that as your starting point, would be:

0 18 * * 1

That would execute at 18:00 on the first day of every week. (Sunday is 0.)

cron has no notion of 'start' and 'stop' dates. Things only ever execute in the future. And when you want them to stop, you remove the entry. It is very primitive, and something like "next-to-last Friday of the month" is cumbersome, but it's amazing what five little numbers can do.


Your cron format is not clear. * * 18 13 07 2009 is not a valid cron entry.

You can use SimpleDateFormat to convert the value to a desired format.

So, in your case, you could use something as below.

String s = "2009-07-13T18:00:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss");
Date d = sf.parse();

//now convert to the format you want
sf = new SimpleDateFormat("* * HH dd mm yyyy");
String cronEntry = sf.format(d);

But before you do this, check your cron entry and find out exactly how you want to setup the cron timing. The example you have given is invalid.

0

精彩评论

暂无评论...
验证码 换一张
取 消