Is there any java class to get Date from cron expression

后端 未结 5 1667
时光说笑
时光说笑 2021-01-04 05:46

I need to find out the first occurrence of Date and time represented by given cron expression. Is there any java class, utility code which can help in getting data object fr

5条回答
  •  余生分开走
    2021-01-04 06:21

    Here's an alternative similar to Quartz's CronExpression but without having to add a fully fledged scheduler to your project: cron-utils

    You can get the date you need with the following:

    //Get date for next execution
    DateTime now = DateTime.now();
    CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
    CronParser parser = new CronParser(cronDefinition);
    ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * * *"));
    DateTime nextExecution = executionTime.nextExecution(now));
    

    According to the official description, cron-utils is:

    A Java library to parse, validate, migrate crons as well as get human readable descriptions for them. The project follows the Semantic Versioning Convention and uses Apache 2.0 license.

提交回复
热议问题