本文整理汇总了Java中com.cronutils.model.definition.CronDefinitionBuilder类的典型用法代码示例。如果您正苦于以下问题:Java CronDefinitionBuilder类的具体用法?Java CronDefinitionBuilder怎么用?Java CronDefinitionBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CronDefinitionBuilder类属于com.cronutils.model.definition包,在下文中一共展示了CronDefinitionBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isValidCron
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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: test
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
@Test
public void test() {
CronDefinition def = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(def);
Cron cron = parser.parse(cronText);
ExecutionTime et = ExecutionTime.forCron(cron);
ZonedDateTime vs = ZonedDateTime.of(2017, 12, 1, 9, 30, 0, 0, ZONE);
assertEquals(DayOfWeek.FRIDAY, vs.getDayOfWeek());
// Last match prior to our reference time
ZonedDateTime expected = ZonedDateTime.of(2017, 11, 30, 18, 15, 0, 0, ZONE);
assertEquals(DayOfWeek.THURSDAY, expected.getDayOfWeek());
Optional<ZonedDateTime> lastExecution = et.lastExecution(vs);
if (lastExecution.isPresent()) {
ZonedDateTime actual = lastExecution.get();
assertEquals(expected, actual);
} else {
fail("last execution was not present");
}
}
示例3: testMustMatchCronEvenIfNanoSecondsVaries
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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: supportQuartzCronExpressionIncrementsOnYears
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #154: Quartz Cron Year Pattern is not fully supported - i.e. increments on years are not supported
* https://github.com/jmrozanec/cron-utils/issues/154
* Duplicate of #148
*/
@Test
public void supportQuartzCronExpressionIncrementsOnYears() {
final String[] sampleCronExpressions = {
"0 0 0 1 * ? 2017/2",
"0 0 0 1 * ? 2017/3",
"0 0 0 1 * ? 2017/10",
"0 0 0 1 * ? 2017-2047/2",
};
final CronParser quartzCronParser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
for (final String cronExpression : sampleCronExpressions) {
final Cron quartzCron = quartzCronParser.parse(cronExpression);
quartzCron.validate();
}
}
示例5: testEveryTwoMinRollsOverHour
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #38: every 2 min schedule doesn't roll over to next hour.
*/
@Test
public void testEveryTwoMinRollsOverHour() {
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
final Cron cron = new CronParser(cronDefinition).parse("*/2 * * * *");
final ExecutionTime executionTime = ExecutionTime.forCron(cron);
final ZonedDateTime time = ZonedDateTime.parse("2015-09-05T13:56:00.000-07:00");
final Optional<ZonedDateTime> nextExecutionTime = executionTime.nextExecution(time);
if (nextExecutionTime.isPresent()) {
final ZonedDateTime next = nextExecutionTime.get();
final Optional<ZonedDateTime> shouldBeInNextHourExecution = executionTime.nextExecution(next);
if (shouldBeInNextHourExecution.isPresent()) {
assertEquals(next.plusMinutes(2), shouldBeInNextHourExecution.get());
return;
}
}
fail("one of the asserted values was not present.");
}
示例6: testMondayWeekdayNextExecution
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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);
}
}
示例7: testNextExecutionDaylightSaving
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #61: nextExecution over daylight savings is wrong.
*/
@Test
public void testNextExecutionDaylightSaving() {
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0 17 * * *"));// daily at 17:00
// Daylight savings for New York 2016 is Mar 13 at 2am
final ZonedDateTime last = ZonedDateTime.of(2016, 3, 12, 17, 0, 0, 0, ZONE_ID_NEW_YORK);
final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(last);
if (nextExecution.isPresent()) {
final long millis = Duration.between(last, nextExecution.get()).toMillis();
assertEquals(23, (millis / 3600000));
assertEquals(last.getZone(), nextExecution.get().getZone());
} else {
fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例8: testLastExecutionDaylightSaving
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #61: lastExecution over daylight savings is wrong.
*/
@Test
public void testLastExecutionDaylightSaving() {
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("0 17 * * *"));// daily at 17:00
// Daylight savings for New York 2016 is Mar 13 at 2am
final ZonedDateTime now = ZonedDateTime.of(2016, 3, 12, 17, 0, 0, 0, ZoneId.of("America/Phoenix"));
final Optional<ZonedDateTime> lastExecution = executionTime.lastExecution(now);
if (lastExecution.isPresent()) {
final long millis = Duration.between(lastExecution.get(), now).toMillis();
assertEquals(24, (millis / 3600000));
assertEquals(now.getZone(), lastExecution.get().getZone());
} else {
fail(LAST_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例9: testNextExecution2014
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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);
}
}
示例10: testWrongNextExecutionOnDSTEnd
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #112: Calling nextExecution exactly on the first instant of the fallback hour (after the DST ends) makes it go back to DST.
* https://github.com/jmrozanec/cron-utils/issues/112
*/
@Test
public void testWrongNextExecutionOnDSTEnd() {
final ZoneId zone = ZoneId.of("America/Sao_Paulo");
//2016-02-20T23:00-03:00[America/Sao_Paulo], first minute of fallback hour
final ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1456020000000L), zone);
final ZonedDateTime expected = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1456020000000L + 60000), zone);
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * *"));
final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
if (nextExecution.isPresent()) {
assertEquals(expected, nextExecution.get());
} else {
fail(NEXT_EXECUTION_NOT_PRESENT_ERROR);
}
}
示例11: testNextExecutionProducesStackTraces
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #125: Prints stack trace for NoSuchValueException for expressions with comma-separated values
* https://github.com/jmrozanec/cron-utils/issues/125
*/
@Test
public void testNextExecutionProducesStackTraces() {
final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("45 1,13 * * *"));
executionTime.nextExecution(ZonedDateTime.parse("2016-05-24T01:02:50Z"));
}
示例12: testCronExpressionEveryTwoHoursAsteriskSlash
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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);
}
}
示例13: testCronExpressionEveryTwoHoursSlash
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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 testCronExpressionEveryTwoHoursSlash() {
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);
}
}
示例14: testMatchWorksAsExpectedForCustomCronsWhenPreviousOrNextOccurrenceIsMissing
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的package包/类
/**
* Issue #136: Bug exposed at PR #136
* https://github.com/jmrozanec/cron-utils/pull/136
* Reported case: when executing isMatch for a given range of dates,
* if date is invalid, we get an exception, not a boolean as response.
*/
@Test
public void testMatchWorksAsExpectedForCustomCronsWhenPreviousOrNextOccurrenceIsMissing() {
final CronDefinition cronDefinition = CronDefinitionBuilder.defineCron()
.withDayOfMonth()
.supportsL().supportsW()
.and()
.withMonth().and()
.withYear()
.and().instance();
final CronParser parser = new CronParser(cronDefinition);
final Cron cron = parser.parse("05 05 2004");
final ExecutionTime executionTime = ExecutionTime.forCron(cron);
ZonedDateTime start = ZonedDateTime.of(2004, 5, 5, 23, 55, 0, 0, ZoneId.of("UTC"));
final ZonedDateTime end = ZonedDateTime.of(2004, 5, 6, 1, 0, 0, 0, ZoneId.of("UTC"));
while (start.compareTo(end) < 0) {
assertTrue(executionTime.isMatch(start) == (start.getDayOfMonth() == 5));
start = start.plusMinutes(1);
}
}
示例15: testThreeRequiredFieldsSupported
import com.cronutils.model.definition.CronDefinitionBuilder; //导入依赖的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");
}