本文整理汇总了Java中com.cronutils.parser.CronParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java CronParser.parse方法的具体用法?Java CronParser.parse怎么用?Java CronParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.cronutils.parser.CronParser
的用法示例。
在下文中一共展示了CronParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isValidCron
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
private boolean isValidCron(String s) {
boolean isValid = false;
CronDefinition cronD =
CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(cronD);
com.cronutils.model.Cron cron = null;
try {
cron = parser.parse(s);
} catch (IllegalArgumentException e) {
LOG.debug("Cron parsing failed!",e);
}
if (cron != null) {
isValid = true;
}
return isValid;
}
示例2: testEveryWednesdayOfEveryDayNextExecution
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #223: for dayOfWeek value == 3 && division of day, nextExecution do not return correct results.
*/
@Test
public void testEveryWednesdayOfEveryDayNextExecution() {
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
final CronParser parser = new CronParser(cronDefinition);
final Cron myCron = parser.parse("* * * * 3");
ZonedDateTime time = ZonedDateTime.parse("2017-09-05T11:31:55.407-05:00");
final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(myCron).nextExecution(time);
if (nextExecution.isPresent()) {
assertEquals(ZonedDateTime.parse("2017-09-06T00:00-05:00"), nextExecution.get());
} else {
fail("next execution was not present");
}
final Cron myCron2 = parser.parse("* * */1 * 3");
time = ZonedDateTime.parse("2017-09-05T11:31:55.407-05:00");
final Optional<ZonedDateTime> nextExecution2 = ExecutionTime.forCron(myCron2).nextExecution(time);
if (nextExecution2.isPresent()) {
assertEquals(ZonedDateTime.parse("2017-09-06T00:00-05:00"), nextExecution2.get());
} else {
fail("next execution was not present");
}
}
示例3: testMustMatchCronEvenIfNanoSecondsVaries
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
@Test
public void testMustMatchCronEvenIfNanoSecondsVaries() {
final CronDefinition cronDefinition =
CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
final CronParser parser = new CronParser(cronDefinition);
final Cron quartzCron = parser.parse("00 00 10 * * ?");
quartzCron.validate();
// NOTE: Off by 3 nano seconds
final ZonedDateTime zdt = ZonedDateTime.of(1999, 07, 18, 10, 00, 00, 03, ZoneId.systemDefault());
// Must be true
assertTrue("Nano seconds must not affect matching of Cron Expressions", ExecutionTime.forCron(quartzCron).isMatch(zdt));
}
示例4: testMondayWeekdayNextExecution
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #45: next execution does not match expected date. Result is not in same timezone as reference date.
*/
@Test
public void testMondayWeekdayNextExecution() {
final String crontab = "* * * * 1";
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
final CronParser parser = new CronParser(cronDefinition);
final Cron cron = parser.parse(crontab);
final ZonedDateTime date = ZonedDateTime.parse("2015-10-13T17:26:54.468-07:00");
final ExecutionTime executionTime = ExecutionTime.forCron(cron);
final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
if (nextExecution.isPresent()) {
assertEquals(ZonedDateTime.parse("2015-10-19T00:00:00.000-07:00"), nextExecution.get());
} else {
fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例5: testAsScheduleExpression
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
@Test
public void testAsScheduleExpression(){
final CronDefinition cronDefinition = TestCronDefinitionsFactory.quartzNoDoWAndDoMRestrictionBothSameTime();
final CronParser cronParser = new CronParser(cronDefinition);
final Cron cron = cronParser.parse("0 * * 1 * MON *");
ScheduleExpression expression = cron.asScheduleExpression();
assertNotNull(expression);
assertEquals(cron.retrieve(CronFieldName.SECOND).getExpression().asString(), expression.getSecond());
assertEquals(cron.retrieve(CronFieldName.MINUTE).getExpression().asString(), expression.getMinute());
assertEquals(cron.retrieve(CronFieldName.HOUR).getExpression().asString(), expression.getHour());
assertEquals(cron.retrieve(CronFieldName.DAY_OF_MONTH).getExpression().asString(), expression.getDayOfMonth());
assertEquals(cron.retrieve(CronFieldName.MONTH).getExpression().asString(), expression.getMonth());
assertEquals(cron.retrieve(CronFieldName.DAY_OF_WEEK).getExpression().asString(), expression.getDayOfWeek());
assertEquals(cron.retrieve(CronFieldName.YEAR).getExpression().asString(), expression.getYear());
}
示例6: testNextExecutionTimeProperlySet
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #123:
* https://github.com/jmrozanec/cron-utils/issues/123
* Reported case: next execution time is set improperly
* Potential duplicate: https://github.com/jmrozanec/cron-utils/issues/124
*/
@Test
public void testNextExecutionTimeProperlySet() {
final CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(QUARTZ));
final String quartzCronExpression2 = "0 5/15 * * * ? *";
final Cron parsedQuartzCronExpression = quartzCronParser.parse(quartzCronExpression2);
final ExecutionTime executionTime = ExecutionTime.forCron(parsedQuartzCronExpression);
final ZonedDateTime zonedDateTime = LocalDateTime.of(2016, 7, 30, 15, 0, 0, 527).atZone(ZoneOffset.UTC);
final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(zonedDateTime);
final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(zonedDateTime);
if (nextExecution.isPresent() && lastExecution.isPresent()) {
final ZonedDateTime nextExecutionTime = nextExecution.get();
final ZonedDateTime lastExecutionTime = lastExecution.get();
assertEquals("2016-07-30T14:50Z", lastExecutionTime.toString());
assertEquals("2016-07-30T15:05Z", nextExecutionTime.toString());
} else {
fail(ASSERTED_EXECUTION_NOT_PRESENT);
}
}
示例7: testNextExecution2014
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #79: Next execution skipping valid date.
*/
@Test
public void testNextExecution2014() {
final String crontab = "0 8 * * 1";//m,h,dom,m,dow ; every monday at 8AM
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
final CronParser parser = new CronParser(cronDefinition);
final Cron cron = parser.parse(crontab);
final ZonedDateTime date = ZonedDateTime.parse("2014-11-30T00:00:00Z");
final ExecutionTime executionTime = ExecutionTime.forCron(cron);
final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
if (nextExecution.isPresent()) {
assertEquals(ZonedDateTime.parse("2014-12-01T08:00:00Z"), nextExecution.get());
} else {
fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例8: testThreeRequiredFieldsSupported
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* A CronDefinition with only 3 required fields is legal to instantiate, but the parser considers an expression
* with 4 fields as an error:
* java.lang.IllegalArgumentException: Cron expression contains 4 parts but we expect one of [6, 7]
*/
//@Test //FIXME issue #291
public void testThreeRequiredFieldsSupported() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().supportsL().supportsW().supportsLW().supportsQuestionMark().optional().and()
.withMonth().optional().and()
.withDayOfWeek().withValidRange(1, 7).withMondayDoWValue(2).supportsHash().supportsL()
.supportsQuestionMark().optional().and()
.withYear().withValidRange(2000, 2099).optional().and()
.instance();
final CronParser cronParser = new CronParser(cronDefinition);
cronParser.parse("* * 4 3");
}
示例9: testCronExpressionEveryTwoHoursAsteriskSlash
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Test for issue #38
* https://github.com/jmrozanec/cron-utils/issues/38
* Reported case: lastExecution and nextExecution do not work properly
* Expected: should return expected date
*/
@Test
public void testCronExpressionEveryTwoHoursAsteriskSlash() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth().and()
.withMonth().and()
.withDayOfWeek().withValidRange(0, 7).withMondayDoWValue(1).withIntMapping(7, 0).and()
.instance();
final CronParser parser = new CronParser(cronDefinition);
final Cron cron = parser.parse("0 0 */2 * * *");
final ZonedDateTime startDateTime = ZonedDateTime.parse("2015-08-28T12:05:14.000-03:00");
final Optional<ZonedDateTime> nextExecution = ExecutionTime.forCron(cron).nextExecution(startDateTime);
if (nextExecution.isPresent()) {
assertTrue(ZonedDateTime.parse("2015-08-28T14:00:00.000-03:00").compareTo(nextExecution.get()) == 0);
} else {
fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例10: validate
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
@Override
public void validate() {
CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
try {
parser.parse(expression);
} catch (IllegalArgumentException iae) {
throw new CronitSystemException("expression.not.valid", expression);
}
}
示例11: initializeExecutionTime
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
private ExecutionTime initializeExecutionTime() {
ExecutionTime executionTime = null;
CronDefinition cronD =
CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(cronD);
Cron cron = parser.parse(cronString);
executionTime = ExecutionTime.forCron(cron);
return executionTime;
}
示例12: cronValidation
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
private static void cronValidation(final String[] args) throws ParseException {
final Options options = new Options();
options.addOption("v", "validate", false, "Action of validation (default)");
options.addOption("f", "format", true,
"Cron expression format to validate. Possible values are: CRON4J, QUARTZ, UNIX");
options.addOption("e", "expression", true, "Cron expression to validate. Example: '* 1 * * *'");
options.addOption("h", HELP, false, "Help");
final String header = "Cron expressions validation by cron-utils\n\n";
final String footer = "\nPlease report issues at https://github.com/jmrozanec/cron-utils/issues";
final CommandLineParser parser = new DefaultParser();
final CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption(HELP) || cmd.getOptions().length == 0) {
showHelp(options, header, footer);
return;
}
if (!cmd.hasOption("validate")) {
showHelp(options, header, footer);
return;
}
if (cmd.hasOption('v')) {
final String format = cmd.getOptionValue("f");
final String expression = cmd.getOptionValue("e");
final CronType cronType = CronType.valueOf(format);
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(cronType);
final CronParser cronParser = new CronParser(cronDefinition);
final Cron quartzCron = cronParser.parse(expression);
quartzCron.validate();
}
}
示例13: testMatchExact
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
@Test
public void testMatchExact() {
final CronDefinition cronDefinition =
CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
final CronParser parser = new CronParser(cronDefinition);
final Cron quartzCron = parser.parse("00 00 10 * * ?");
quartzCron.validate();
final ZonedDateTime zdt = ZonedDateTime.of(1999, 07, 18, 10, 00, 00, 00, ZoneId.systemDefault());
assertTrue("Nano seconds must not affect matching of Cron Expressions", ExecutionTime.forCron(quartzCron).isMatch(zdt));
}
示例14: exactHitReturnsFullIntervalDuration
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #130: Wrong last execution time if schedule hit is less than one second ago
* https://github.com/jmrozanec/cron-utils/issues/130
*/
@Test
public void exactHitReturnsFullIntervalDuration() {
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final Cron cron = parser.parse("0 12 * * *");
final ZonedDateTime time = ZonedDateTime.of(2016, 12, 2, 12, 0, 0, 0, ZoneId.of("Europe/Vienna"));
final Optional<Duration> timeFromLastExecution = ExecutionTime.forCron(cron).timeFromLastExecution(time);
if (timeFromLastExecution.isPresent()) {
assertEquals(timeFromLastExecution.get(), Duration.ofHours(24));
} else {
fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例15: fuzzyHitReturnsVerySmallIntervalDuration
import com.cronutils.parser.CronParser; //导入方法依赖的package包/类
/**
* Issue #130: Wrong last execution time if schedule hit is less than one second ago
* https://github.com/jmrozanec/cron-utils/issues/130
*/
@Test
public void fuzzyHitReturnsVerySmallIntervalDuration() {
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final Cron cron = parser.parse("0 12 * * *");
ZonedDateTime time = ZonedDateTime.of(2016, 12, 2, 12, 0, 0, 0, ZoneId.of("Europe/Vienna"));
final Duration diff = Duration.ofMillis(300);
time = time.plus(diff);
final Optional<Duration> timeFromLastExecution = ExecutionTime.forCron(cron).timeFromLastExecution(time);
if (timeFromLastExecution.isPresent()) {
assertEquals(timeFromLastExecution.get(), Duration.ofDays(1).plus(diff));
} else {
fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
}
}