如何将Java的cron表达式转换为“每天次数”值?

huangapple go评论118阅读模式
英文:

How do I convert java cron expressions into a 'number of times per day' value?

问题

我有许多服务,它们每天都基于传递给它们的 cron 值运行。我正在开发一个需要知道每个服务每天执行次数的服务。是否可能在特定于 Java 1.8 的情况下将 cron 表达式转换为“每天/每周次数”?仅供参考,我在一个 Spring Boot 项目中进行开发,以防这在某种程度上产生任何差异。

例如,假设我有一个表达式像 0 1 0-20/4 * * *。那个任务每天会运行 5 次。我有这样的想法:

float dailyUploads = someLibrary.cronToFloat("0 1 0-20/4 * * *");

其中 dailyUploads == 5.0

当我在谷歌上搜索时,大多数结果只涉及创建 cron 表达式。我确实找到了一个关于将 cron 转换为日期时间值的有趣帖子。

我意识到我可以使用 cron 序列生成器(在上面链接的答案中提到)生成直到日期更改的序列,然后对结果进行计数,但这只为我提供了那一天的结果;那么对于每年运行一次的服务呢?我只是找不到一个简洁的方法来做到这一点。

感谢您的任何帮助。

英文:

I have many services that run on a daily basis all based on cron values passed to them. I'm working on a service that needs to know the number of times per day each service does its job. Is it possible to convert a cron expression into a 'number per day/week' in Java 1.8 specifically? Just fyi, I'm in a spring boot project for this, in case that somehow makes any difference.

For example, assume I have an expression like 0 1 0-20/4 * * *. That job would run 5 times every day. I had something in mind like:


float dailyUploads = someLibrary.cronToFloat("0 1 0-20/4 * * *");

where dailyUploads == 5.0

When I google this, most results only deal with creating cron expressions. I did find one interesting post about converting cron to date-time values.

It occurs to me that I could use a cron sequence generator (mentioned in the above-linked answer) to sequence until the day changes followed by counting the results, but that only gives me the results for that one day; what about a service that runs once every year? I just can't find a clean way to do this.

Thank you for any assistance.

答案1

得分: 2

import com.cronutils.descriptor.CronDescriptor;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;

import java.util.Locale;

import static com.cronutils.model.CronType.QUARTZ;

// ...

final String expr = "0 * * 1-3 * ? *";

CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);

CronParser parser = new CronParser(cronDefinition);

CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);

String description = descriptor.describe(parser.parse(expr));

The output from that would be the following:

every minute every day between 1 and 3

英文:

One approach is to use a library like http://cron-parser.com/. With that, you could have code that looks something like this:

import com.cronutils.descriptor.CronDescriptor;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;

import java.util.Locale;

import static com.cronutils.model.CronType.QUARTZ;

// ...

    final String expr = "0 * * 1-3 * ? *";
    
    CronDefinition cronDefinition =
            CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);

    CronParser parser = new CronParser(cronDefinition);

    CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);

    String description = descriptor.describe(parser.parse(expr));

The output from that would be the following:

every minute every day between 1 and 3

答案2

得分: 1

cron-utils也可以完美运行

英文:

cron-utils would work perfectly too

huangapple
  • 本文由 发表于 2020年10月28日 02:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64560853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定