All Projects → jmrozanec → Cron Utils

jmrozanec / Cron Utils

Licence: apache-2.0
Cron utils for parsing, validations and human readable descriptions as well as date/time interoperability.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Cron Utils

Quartznet
Quartz Enterprise Scheduler .NET
Stars: ✭ 4,825 (+566.44%)
Mutual labels:  hacktoberfest, cron, quartz
Go Quartz
Simple, zero-dependency scheduling library for Go
Stars: ✭ 118 (-83.7%)
Mutual labels:  cron, crontab, quartz
Cron Editor
cron editor
Stars: ✭ 22 (-96.96%)
Mutual labels:  cron, crontab, quartz
mi-cron
📆 A microscopic parser for standard cron expressions.
Stars: ✭ 16 (-97.79%)
Mutual labels:  cron, crontab
LexikCronFileGeneratorBundle
This symfony bundle provides service for generate cron file
Stars: ✭ 20 (-97.24%)
Mutual labels:  cron, crontab
job-plus
Job Plus项目是基于SpringBoot+Vue的轻量级定时任务管理系统
Stars: ✭ 17 (-97.65%)
Mutual labels:  cron, quartz
Automation-using-Shell-Scripts
Development Automation using Shell Scripting.
Stars: ✭ 41 (-94.34%)
Mutual labels:  cron, crontab
php-cron-expr
Ultra lightweight, Dependency free and Super Fast Cron Expression parser for PHP
Stars: ✭ 42 (-94.2%)
Mutual labels:  cron, crontab
lambda-cron
LambdaCron - serverless cron tool
Stars: ✭ 22 (-96.96%)
Mutual labels:  cron, crontab
crony
Manage remote crontabs from your terminal
Stars: ✭ 12 (-98.34%)
Mutual labels:  cron, crontab
Gocron
定时任务管理系统
Stars: ✭ 4,198 (+479.83%)
Mutual labels:  cron, crontab
Chronos
Fault tolerant job scheduler for Mesos which handles dependencies and ISO8601 based schedules
Stars: ✭ 4,303 (+494.34%)
Mutual labels:  cron, crontab
jobor
支持秒级分布式定时任务系统, A high performance distributed task scheduling system, Support multi protocol scheduling tasks
Stars: ✭ 52 (-92.82%)
Mutual labels:  cron, quartz
croncal
Utility to convert a crontab file to a list of actual events within a date range.
Stars: ✭ 37 (-94.89%)
Mutual labels:  cron, crontab
asparagus
An easy to use task scheduler for distributed systems
Stars: ✭ 14 (-98.07%)
Mutual labels:  cron, crontab
crontab
cron expression parser and executor for dotnet core.
Stars: ✭ 13 (-98.2%)
Mutual labels:  cron, crontab
cronitor-cli
Command line tools for Cronitor.io
Stars: ✭ 31 (-95.72%)
Mutual labels:  cron, crontab
Cron Expression Descriptor
A .NET library that converts cron expressions into human readable descriptions.
Stars: ✭ 602 (-16.85%)
Mutual labels:  cron, quartz
node-cron-expression
Declarative functional cron expression builder
Stars: ✭ 17 (-97.65%)
Mutual labels:  cron, crontab
watchman
📆 更夫(watchman)是一款可视化的定时任务配置 Web 工具,麻麻不用担心我漏掉任何更新啦!
Stars: ✭ 40 (-94.48%)
Mutual labels:  cron, crontab

cron-utils

We define crons. And support them.

cron-utils is a Java library to define, parse, validate, migrate crons as well as get human readable descriptions for them. The project follows the Semantic Versioning Convention, provides OSGi metadata and uses Apache 2.0 license.

Gitter Chat Build Status Coverage Status

Codacy Badge Project stats by OpenHub

Download

cron-utils is available on Maven central repository.

<dependency>
    <groupId>com.cronutils</groupId>
    <artifactId>cron-utils</artifactId>
    <version>9.1.3</version>
</dependency>

For Android developers, cron-utils 7.0.0 assumes Android 26+. For earlier Android versions consider using cron-utils 6.0.6. If using ScheduleExpression from Java EE, this should be provided as a runtime dependency.

Current development

Now we are developing a new generation of cron-descriptors using neural-translation! Any kind of contributions are welcome: from help with dataset generation to machine learning models training and utilities to load them! If interested, please follow issue #3

Features

  • Create arbitrary cron expressions: you can define your own cron format! Supported fields are: second, minute, hour, day of month, month, day of week, year.
  • You can flag last field as optional!
  • Supports all cron special characters: * / , -
    • Non-standard characters L, W, LW, '?' and # are supported as well!
  • Print to locale specific human readable format (Chinese, English, German, Greek, Indonesian, Korean, Polish, Spanish, Swahili and Turkish are fully supported. Dutch, French, Italian, Portuguese and Russian have basic support).
  • Parse and Description process are decoupled: parse once and operate with the result!
  • Build cron expressions using CronBuilder:
    • no need to remember fields and constraints for each cron provider
    • crons become decoupled from cron provider: anytime you can export to another format.
  • Check if cron expressions are equivalent
  • Squash multiple cron expressions into a single one!
  • Validate if cron string expressions match a cron definition
  • Convert crons between different cron definitions: if you need to migrate expressions, CronMapper may help you!
  • Pre-defined definitions for the following cron libraries are provided:
  • Obtain last/next execution time as well as time from last execution/time to next execution.
  • Obtain weekdays count between two dates, considering different weekend policies as well as holidays.
  • Need to map constants between different cron/time libraries? Use ConstantsMapper.

Usage Examples

Below we present some examples. You can find this and others in a sample repo we created to showcase cron-utils libraries!

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().optional().and()
        .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 * ? *

Parse

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

... even multi-cron expressions! How about squashing multiple crons into a single line? Instead of writing 0 0 9 * * ? *, 0 0 10 * * ? *, 0 30 11 * * ? * and 0 0 12 * * ? * we can wrap it into 0 0|0|30|0 9|10|11|12 * * ? *

Describe

// 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"

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

// 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

Validate

cron4jCron.validate()

Calculate time from/to execution

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

// Get date for next execution
ZonedDateTime nextExecution = executionTime.nextExecution(now);

// Time from last execution
Duration timeFromLastExecution = executionTime.timeFromLastExecution(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
        );

Date and time formatting for humans!

Use 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 = 
	    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

cron-utils CLI

We provide a simple CLI interface to use cron-utils right from console, without writing a new project! The CLI is a satellite project, available at cron-utils-cli

  • Usage: java -jar cron-utils.jar com.cronutils.cli.CronUtilsCLI --validate -f [CRON4J|QUARTZ|UNIX] -e '<cron expression>'

  • Example: java -jar cron-utils.jar com.cronutils.cli.CronUtilsCLI --validate -f UNIX -e '* 1 * * *'

If you want a standalone jar without requiring the 'cp', build an uber jar with :

mvn assembly:assembly -DdescriptorId=jar-with-dependencies

Then, launch cli-utils (built in the target directory) with :

java -jar cron-utils-<version>-jar-with-dependencies.jar com.cronutils.cli.CronUtilsCLI --validate -f [CRON4J|QUARTZ|UNIX] -e '<cron expression>'`

Contribute & Support!

Contributions are welcome! You can contribute by

  • starring and/or Flattring this repo!
  • requesting or adding new features. Check our roadmap!
  • enhancing existing code: ex.: provide more accurate description cases
  • testing
  • enhancing documentation
  • providing translations to support new locales
  • bringing suggestions and reporting bugs
  • spreading the word
  • telling us how you use it! We look forward to list you at our wiki!

Check our page! For stats about the project, you can visit our OpenHUB profile.

Support us donating once or by subscription through Flattr!

Flattr this!

Other cron-utils projects

You are welcome to visit and use the following cron-utils projects:

  • htime: A Java library to make it easy for humans format a date. You no longer need to remember date time formatting chars: just write an example, and you will get the appropriate formatter.
  • cron-utils-spring: A Java library to describe cron expressions in human readable language at Spring framework, using cron-utils.
  • cron-utils-cli: cron-utils features made available through a CLI.
  • cron-utils-sisyphus: A Scala scheduler that supports multiple cron notations.
  • cron-utils-scheduler: A Java job scheduler based on cron-utils library.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].