java中根据秒生成cron表达式,是否有用于创建Cron表达式的Java代码?

I need a Java code to create a cron expression based on user inputs.

User inputs are Time, frequency and number of executions.

解决方案

Just adding on from the comment that says create it yourself.

Here is an example: Prompt the user for the values and pass them into the following method, the Javadoc explains what is allowed in which value (taken from http://en.wikipedia.org/wiki/Cron#Format).

This is untested and doesn't validate any of the input strings, but I'm sure you can do that.

/**

* Generate a CRON expression is a string comprising 6 or 7 fields separated by white space.

*

* @param seconds mandatory = yes. allowed values = {@code 0-59 * / , -}

* @param minutes mandatory = yes. allowed values = {@code 0-59 * / , -}

* @param hours mandatory = yes. allowed values = {@code 0-23 * / , -}

* @param dayOfMonth mandatory = yes. allowed values = {@code 1-31 * / , - ? L W}

* @param month mandatory = yes. allowed values = {@code 1-12 or JAN-DEC * / , -}

* @param dayOfWeek mandatory = yes. allowed values = {@code 0-6 or SUN-SAT * / , - ? L #}

* @param year mandatory = no. allowed values = {@code 1970–2099 * / , -}

* @return a CRON Formatted String.

*/

private static String generateCronExpression(final String seconds, final String minutes, final String hours,

final String dayOfMonth,

final String month, final String dayOfWeek, final String year)

{

return String.format("%1$s %2$s %3$s %4$s %5$s %6$s %7$s", seconds, minutes, hours, dayOfMonth, month, dayOfWeek, year);

}

Cron Format Information taken from here: http://en.wikipedia.org/wiki/Cron#Format

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值