voidburn / cron-expression-descriptor

A Java library that converts cron expressions into human readable descriptions
https://github.com/voidburn/cron-expression-descriptor
MIT License
27 stars 11 forks source link
cron cron-expression cron-libraries human-readable-descriptions quartz-scheduler

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

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:

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"