Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jmrozanec/cron-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed Aug 3, 2016
2 parents 105f2b6 + 6b87973 commit 767bad0
Showing 1 changed file with 89 additions and 81 deletions.
170 changes: 89 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,116 +46,124 @@ Below we present some examples. You can find this and others in a [sample repo w

***Build cron definitions***

//define your own cron: arbitrary fields are allowed and last field can be optional
CronDefinition cronDefinition =
CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth()
.supportsHash().supportsL().supportsW().and()
.withMonth().and()
.withDayOfWeek()
.withIntMapping(7, 0) //we support non-standard non-zero-based numbers!
.supportsHash().supportsL().supportsW().and()
.withYear().and()
.lastFieldOptional()
.instance();

//or get a predefined instance
cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
```java
// Define your own cron: arbitrary fields are allowed and last field can be optional
CronDefinition cronDefinition =
CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth()
.supportsHash().supportsL().supportsW().and()
.withMonth().and()
.withDayOfWeek()
.withIntMapping(7, 0) //we support non-standard non-zero-based numbers!
.supportsHash().supportsL().supportsW().and()
.withYear().and()
.lastFieldOptional()
.instance();

// or get a predefined instance
cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
```

***Build a cron expression***

//Create a cron expression. CronMigrator will ensure you remain cron provider agnostic
import static com.cronutils.model.field.expression.FieldExpressionFactory.*;
Cron cron = CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withYear(always())
.withDoM(between(SpecialChar.L, 3))
.withMonth(always())
.withDoW(questionMark())
.withHour(always())
.withMinute(always())
.withSecond(on(0))
.instance();
//Obtain the string expression
String cronAsString = cron.asString();//0 * * L-3 * ? *

```java
// Create a cron expression. CronMigrator will ensure you remain cron provider agnostic
import static com.cronutils.model.field.expression.FieldExpressionFactory.*;

Cron cron = CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withYear(always())
.withDoM(between(SpecialChar.L, 3))
.withMonth(always())
.withDoW(questionMark())
.withHour(always())
.withMinute(always())
.withSecond(on(0))
.instance();
// Obtain the string expression
String cronAsString = cron.asString(); // 0 * * L-3 * ? *
```

***Parse***

//create a parser based on provided definition
CronParser parser = new CronParser(cronDefinition);
Cron quartzCron = parser.parse("0 23 * ? * 1-5 *");
```java
// Create a parser based on provided definition
CronParser parser = new CronParser(cronDefinition);
Cron quartzCron = parser.parse("0 23 * ? * 1-5 *");
```

***Describe***
```java
// Create a descriptor for a specific Locale
CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);

//create a descriptor for a specific Locale
CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);

//parse some expression and ask descriptor for description
String description = descriptor.describe(parser.parse("*/45 * * * * ?"));
//description will be: "every 45 seconds"
// Parse some expression and ask descriptor for description
String description = descriptor.describe(parser.parse("*/45 * * * * ?"));
// Description will be: "every 45 seconds"

description = descriptor.describe(quartzCron);
//description will be: "every hour at minute 23 every day between Monday and Friday"
//which is the same description we get for the cron below:
descriptor.describe(parser.parse("0 23 * ? * MON-FRI *"));
description = descriptor.describe(quartzCron);
// Description will be: "every hour at minute 23 every day between Monday and Friday"
// which is the same description we get for the cron below:
descriptor.describe(parser.parse("0 23 * ? * MON-FRI *"));
```

***Migrate***
```java
// Migration between cron libraries has never been so easy!
// Turn cron expressions into another format by using CronMapper:
CronMapper cronMapper = CronMapper.fromQuartzToCron4j();

//Migration between cron libraries has never been so easy!
//Turn cron expressions into another format by using CronMapper:
CronMapper cronMapper = CronMapper.fromQuartzToCron4j();

Cron cron4jCron = cronMapper.map(quartzCron);
//and to get a String representation of it, we can use
cron4jCron.asString();//will return: 23 * * * 1-5
Cron cron4jCron = cronMapper.map(quartzCron);
// and to get a String representation of it, we can use
cron4jCron.asString();//will return: 23 * * * 1-5
```

***Validate***

cron4jCron.validate()
```java
cron4jCron.validate()
```

***Calculate time from/to execution***
```java
// Get date for last execution
DateTime now = DateTime.now();
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
DateTime lastExecution = executionTime.lastExecution(now));

//Get date for last execution
DateTime now = DateTime.now();
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
DateTime lastExecution = executionTime.lastExecution(now));
// Get date for next execution
DateTime nextExecution = executionTime.nextExecution(now));

//Get date for next execution
DateTime nextExecution = executionTime.nextExecution(now));
// Time from last execution
Duration timeFromLastExecution = executionTime.timeFromLastExecution(now);

//Time from last execution
Duration timeFromLastExecution = executionTime.timeFromLastExecution(now);

//Time to next execution
Duration timeToNextExecution = executionTime.timeToNextExecution(now);
// Time to next execution
Duration timeToNextExecution = executionTime.timeToNextExecution(now);
```

***Map constants between libraries***

//Map day of week value from Quartz to JodaTime
int jodatimeDayOfWeek =
ConstantsMapper.weekDayMapping(
ConstantsMapper.QUARTZ_WEEK_DAY,
ConstantsMapper.JODATIME_WEEK_DAY
);

```java
// Map day of week value from Quartz to JodaTime
int jodatimeDayOfWeek =
ConstantsMapper.weekDayMapping(
ConstantsMapper.QUARTZ_WEEK_DAY,
ConstantsMapper.JODATIME_WEEK_DAY
);
```
***Date and time formatting for humans!***

Use [htime](https://github.com/jmrozanec/htime) - Human readable datetime formatting for Java!
Despite this functionality is not bundled in the same jar, is a cron-utils project you may find useful.

//You no longer need to remember "YYYY-MM-dd KK a" patterns.
DateTimeFormatter formatter =
```java
// You no longer need to remember "YYYY-MM-dd KK a" patterns.
DateTimeFormatter formatter =
HDateTimeFormatBuilder
.getInstance()
.forJodaTime()
.getFormatter(Locale.US)
.forPattern("June 9, 2011");
String formattedDateTime = formatter.print(lastExecution);
//formattedDateTime will be lastExecution in "dayOfWeek, Month day, Year" format

String formattedDateTime = formatter.print(lastExecution);
// formattedDateTime will be lastExecution in "dayOfWeek, Month day, Year" format
```

**Contribute & Support!**

Expand Down

0 comments on commit 767bad0

Please sign in to comment.