Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

A Java library that converts cron expressions into human readable descriptions

License

Notifications You must be signed in to change notification settings

voidburn/cron-expression-descriptor

Repository files navigation

Archived

Due to general lack of time, and limited interest, I am archiving this repository. No further versions will be updated on Maven. If you require customization you should include the cron and utils folders directly into your project.

Cron Expression Descriptor

A Java library that converts cron expressions into human readable descriptions. Adapted from original work by Brady Holt (https://github.com/bradymholt/cron-expression-descriptor)

Build

License

Licensed under the MIT license

Features

  • Supports all cron expression special characters including * / , - ? L W, #
  • Supports 5, 6 (w/ seconds or year), or 7 (w/ seconds and year) part cron expressions
  • Supports Quartz Enterprise Scheduler cron expressions
  • Localization with support for 22 languages

Add it to your project!

Maven

Add the following dependency to your pom.xml:

<dependency>
  <groupId>it.burning</groupId>
  <artifactId>cron-expression-descriptor</artifactId>
  <version>1.2.10</version>
</dependency>

Gradle

Add the repositories and dependency to your gradle.build script:

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}

dependencies {
    implementation "it.burning:cron-expression-descriptor:1.2.10"
}

Options

A CronExpressionParser.Options object can be passed to CronExpressionDescriptor.getDescription(). The following options are available:

  • boolean throwExceptionOnParseError - If exception occurs when trying to parse expression and generate description, whether to throw or catch and output the Exception message as the description. (Default: true)
  • boolean verbose - Whether to use a verbose description (Default: false)
  • boolean use24HourTimeFormat - If true, descriptions will use a 24-hour clock (Default: true)
  • boolean useJavaEeScheduleExpression - If true, expressions with 5, 6 and 7 parts will all consider 0 and 7 as SUNDAY for the Day Of Week
  • string locale - The locale to use (Default: current system locale)

Example usage with default options:

CronExpressionDescriptor.getDescription("0 0 12 * * ?");
> "At 12:00, every day"

Example usage with custom options:

CronExpressionDescriptor.getDescription("0 0 12 * * ?",new Options(){{
       setLocale("it");
       setUse24HourTimeFormat(false);
       }});
       >"Alle 12:00 PM, ogni giorno"

Example usage for JEE Timer expressions [ Available Since Version 1.2.6+ ] (https://docs.oracle.com/javaee/7/tutorial/ejb-basicexamples004.htm):

CronExpressionDescriptor.getDescription("0 0 13 * * 0",new Options(){{
       setUseJavaEeScheduleExpression(true);
       }});
       >"At 13:00, only on Sunday"
CronExpressionDescriptor.getDescription("0 0 13 * * 7",new Options(){{
        setUseJavaEeScheduleExpression(true);
        }});
        >"At 13:00, only on Sunday"
CronExpressionDescriptor.getDescription("0 0 13 * * 1",new Options(){{
        setUseJavaEeScheduleExpression(true);
        }});
        >"At 13:00, only on Monday"

Please Note: Default options are cached internally, but if you want to use a custom Options set it is advisable to instantiate it only once and reuse it on every subsequent call to avoid useless allocation.

i18n

The following language translations are available.

If you want to manually set a default Locale for the descripion, to be used for all subsequent calls to " getDescription()" you can use the static method "setDefaultLocale()" by passing it the language identifier:

CronExpressionDescriptor.setDefaultLocale("it");
CronExpressionDescriptor.getDescription("0-10 11 * * *");
> "Alle 12:00, ogni giorno"

And you can revert at any time to the default Locale:

For Version 1.2.3+

CronExpressionDescriptor.setDefaultLocale();
CronExpressionDescriptor.getDescription("0-10 11 * * *");
 > "At 12:00, every day"

Up until Version 1.2.2

CronExpressionDescriptor.setDefaultLocale(null);
CronExpressionDescriptor.getDescription("0-10 11 * * *");
 > "At 12:00, every day"