将 cron 表达式转换为漂亮的描述字符串?有 JAVA 和 Objective-C 的库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4469276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:31:55  来源:igfitidea点击:

Convert cron expression into nice description strings? Is there a library for JAVA and Objective-C?

javaobjective-cdatetimecroncronexpression

提问by OneWorld

I am looking for a parser that converts a cron expressionlike 45 17 7 6 * *into Every year, on June 7th at 17:45The parser should be adjustable to other languages. German for the first step.

我要寻找的是一个转换解析器cron表达式45 17 7 6 * *在17:45,每年6月7日的解析器应调整为其他语言。第一步是德语。

Is there a library for a

有图书馆吗

  • JAVA based Android project
  • Objective-C based Iphone project.
  • 基于JAVA的Android项目
  • 基于 Objective-C 的 Iphone 项目。

See herefor the usecase.

有关用例,请参见此处

回答by Jigar Joshi

cronTrigger.getExpressionSummary()

Example:

例子:

    CronTrigger t = new CronTrigger();
    t.setCronExpression("0 30 10-13 ? * WED,FRI");
    System.out.println(""+t.getExpressionSummary());

Output:

输出:

seconds: 0
minutes: 30
hours: 10,11,12,13
daysOfMonth: ?
months: *
daysOfWeek: 4,6
lastdayOfWeek: false
nearestWeekday: false
NthDayOfWeek: 0
lastdayOfMonth: false
years: *

回答by robsf

In Java, have a look into cron4j http://www.sauronsoftware.it/projects/cron4j/

在 Java 中,查看 cron4j http://www.sauronsoftware.it/projects/cron4j/

You will find the parser you need but then you have to write your code to print the string as you need it. Start by creating a SchedulingPattern object:

您将找到所需的解析器,但随后您必须编写代码以根据需要打印字符串。首先创建一个 SchedulingPattern 对象:

new SchedulingPattern("0 30 10-13 ? * 1,2,5")

回答by kekec

You may find cron-utilsuseful for this task, since provides human readable descriptions in various languages and does not require a fully fledged scheduler to provide them. Supports multiple cron formats. Below a code snippet from the docs:

您可能会发现cron-utils对这项任务很有用,因为它以各种语言提供人类可读的描述,并且不需要完全成熟的调度程序来提供它们。支持多种 cron 格式。在文档中的代码片段下方:

//create a descriptor for a specific Locale
CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);

//parse some expression and ask descriptor for description
String description = descriptor.describe(parser.parse("*/45 * * * * *"));
//description will be: "every 45 seconds"