java date转换成cron表达式,如何将cron表达式转换为日期或时间

I am trying to convert the cron expression to date.

for example this is 0 0 23 1 1-12 ? expression. I want to convert to corresponding date.

How do I achieve this?

This is what I tried but it just give the description, Human readable but I want to achieve proper date format.

import java.text.ParseException;

import java.util.Date;

import org.quartz.CronExpression;

public class CRON {

public static void main(String args[]) throws ParseException

{

//AutoRegistrationJobDao obj=new AutoRegistrationJobDao();

//obj.updateLastAutoRegSchdl();

String cron ="0 0 23 1 1-12 ?";

System.out.println("cron :"+cron);

CronExpression cronExpression =new CronExpression("0 0 23 1 1-12 ?");

System.out.println(cronExpression.getExpressionSummary());

System.out.println(cronExpression.getFinalFireTime());

//System.out.println(cronExpression.getNextValidTimeAfter(date));

try {

CronExpression c =new CronExpression(cron);

Date date=c.getFinalFireTime();

System.out.println(date);

System.out.println(c.getFinalFireTime());

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

This is what I tried, I am using multiple method and using different class, but no luck

Output:

seconds: 0

minutes: 0

hours: 23

daysOfMonth: 1

months: 1,2,3,4,5,6,7,8,9,10,11,12

daysOfWeek: ?

lastdayOfWeek: false

nearestWeekday: false

NthDayOfWeek: 0

lastdayOfMonth: false

解决方案

*/20 * * * * command -> this will execute once in 20 min. This cannot be mapped to a single point of time.

Given a cron expression - there will be multplie instance in time when the job should get triggered. So you cannot expect a one to one mapping b/w the cron and a date instance.

If you have a date reference, you can figure out the next job trigger time or the like from the reference date.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用一个名为"CronToExpression"的Java库将时间转换cron表达式。这个库支持所有cron表达式的特殊字符,包括 * / - ? LW #。您可以通过传入日期来获取相应的cron表达式。下面是一个示例代码: ```java import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); String cron = CronToExpression.getCron(date); System.out.println("当前时间 " + date.toString() + " 的表达式为 " + cron); } } ``` 这个示例将当前时间转换cron表达式。您可以根据您的需求更改日期参数来转换不同的时间。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [cron-parser:Cron表达式Java解析器](https://download.csdn.net/download/weixin_42131628/18342207)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Java根据指定时间生成cron表达式](https://blog.csdn.net/qq_43102730/article/details/125289153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值