是否有用于创建Cron表达式的Java代码? [英] Is there any Java code for creating Cron Expression?

查看:595
本文介绍了是否有用于创建Cron表达式的Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Java代码才能根据用户输入创建cron表达式。
用户输入的是时间,频率和执行次数。

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.

这里是一个示例:提示用户输入值并将它们传递给以下方法,Javadoc解释了允许使用哪个值(取自 http://en.wikipedia.org/wiki/Cron#Format )。

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格式信息取自此处: http://en.wikipedia.org/wiki/Cron#Format

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

这篇关于是否有用于创建Cron表达式的Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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