Saturday, January 27, 2024

[SOLVED] Is there a way to print cron-expressions in human readable form?

Issue

Is there some way in java, a library or similar to interpret a cron-expression to human-readable format? I was thinking using it to log how often a job is run to potentially spot wrong cron-expressions.


Solution

You could use cron-parser:

<dependency>
    <groupId>net.redhogs.cronparser</groupId>
    <artifactId>cron-parser</artifactId>
    <version>3.5</version>
</dependency>

Then, you can use the CronExpressionDescriptor class to get human-readable descriptions e.g. CronExpressionDescriptor.getDescription("46 9 * * 1") gives "At 9:46 AM, only on Monday".

refs:

https://central.sonatype.dev/artifact/net.redhogs.cronparser/cron-parser/3.5 https://github.com/grahamar/cron-parser https://github.com/grahamar/cron-parser/blob/master/cron-parser-core/src/test/java/net/redhogs/cronparser/CronExpressionDescriptorTest.java (contains comprehensive usage cases)



Answered By - Shaun Phillips
Answer Checked By - Willingham (WPSolving Volunteer)