cron expression parsing into java date - cron

my database having 10 18 16 ? * SUN,MON,WED,FRI * cron expression then how to convert into Java date.
how to comparing with present day time.
and one more is how to compare to cron expressions i.e. 10 18 16 ? * SUN,MON,WED,FRI * and 0 30 9 30 * ?
please explain the sample code using quartz or spring scheduling.

Please use:
import org.springframework.scheduling.support.CronSequenceGenerator;
final String cronExpression = "0 45 23 * * *";
final CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
final Date nextExecutionDate = generator.next(new Date());
...and then I suggest use Joda DateTime for date comparison.

I wrote a small class for handling cron expressions, available here:
https://github.com/frode-carlsen/cron
Based on Joda-time, but should be fairly easy to port to Java8 time api. This also makes it possible to embed in unit tests, do simulations etc by adjusting the DateTime offset in Joda-time.
It also has pretty good test coverage (was done as TDD Kata).
Update
Now supports java 8 time api as well thanks to a contribution from github user https://github.com/zemiak. In both cases, the expression parser is a single, tiny class which can easily be copied into your own project.

You may want to look into the org.quartz.CronExpression class in the Quartz API.
Please note that you cannot simply compare a cron expression with a date because the cron expression (typically) represents a sequence of various dates. In any case, you may find the following methods useful:
public boolean isSatisfiedBy(Date date)
public Date getNextValidTimeAfter(Date date)
As for comparing two cron expressions, what would you like to compare? The only thing that IMO makes sense to compare are the next 'trigger' dates, i.e. dates obtained from getNextValidTimeAfter([some reference date]) calls.

Perhaps you can check cron-utils
It has some utils to get next/previous execution given certain date, ex.: now. Works with JodaTime, but you could retrieve a JavaDate from there.
The library is scheduler agnostic: you just provide a string with a cron expression.
Is compatible with JDK6.

Related

How do i run a cron schedule a DAG in airflow so that it runs at 10:30 cst on first of one month and 10 cst on the other days of the month ? I have tr

Like i have tried multiple combinations in cron guru but havent found any possible solution.
I have these two expressions to combine
30 10 1 * *
10 12 2-31 * *
You cannot implement this in one cron expression, you have two options to do that:
easy one: create two copy of the dag, each one with its own cron expression
hard one: create a Timetable and register it in an Airflow plugin, then you can use pass it to your dag as timetable argument instead of schedule_interval.
In this timetable, you can implement two methods:
next_dagrun_info: The scheduler uses this to learn the timetable’s regular schedule
infer_manual_data_interval: When a DAG run is manually triggered (from the web UI, for example), the scheduler uses this method to learn about how to reverse-infer the out-of-schedule run’s data interval (not mandatory).

Set Azure Batch MaxWallClockTime Node SDK

I'm trying to set a maxWallClockTime of 72 hours using the ISO 8601 Duration format. The documentation for this property is useless, so I'm basing my guess on using the 8601 format on that being the way to set the same property at the Batch Job level when using the CLI. My constraints object is as follows:
const taskConstraints = {
maxWallClockTime: 'P3D' //ISO 8601 Duration Format e.g. P3Y6M4DT12H30M5S represents a duration of three years, six months, four days, twelve hours, thirty minutes, and five seconds.
};
However, this results in the following error:
task.constraints.maxWallClockTime must be a TimeSpan/Duration.
I cannot find any examples that set this property and use the Javascript API, any pointers to better documentation or example code would be greatly appreciated.
Agreed the docs are lacking here. I haven't tested this out locally yet, but from looking at the code I believe the answer depends on whether you are using the older Node.js-specific azure-batch package or the newer #azure/batch which also runs in web browsers.
For the "azure-batch" package, it looks like it takes a Moment.js duration object. Here's the related JSDoc string:
* #property {moment.duration} [maxWallClockTime] The maximum elapsed time
* that the Task may run, measured from the time the Task starts. If the Task
* does not complete within the time limit, the Batch service terminates it.
* If this is not specified, there is no time limit on how long the Task may
* run.
For the newer "#azure/batch" package, it should take an ISO-8601 duration string. If you're using that package then the value you're trying to use looks right to me, and maybe it's a bug (I'd have to try to repro it).

Node.js Storing Current Date as Epoch Value

I know there are many questions relative to this, but I can't find exactly what I am looking for..
I am creating an iOS Rideshare app and am utilizing the Google Distance Matrix. What I am looking to do is taking the current date and set it to midnight. For example: currentDate = 12/6/2018 12:00:00.
I want to take this currentDate value and convert is to Epoch and set it up as a baseEpoch value. This way, I can take the user's time input, and add the difference to get the date/time they entered in Epoch form.
I've tried solutions such as:
function convertToEpoch(time)
{
var sep = time.split(':');
var seconds = (+sep[0]) * 60 * 60 + (+sep[1]) * 60 + (+sep[2]);
return seconds;
}
function currentDateAsEpoch(time) {
var time = new Date();
time.setHours(0,0,0,0);
convertToEpoch(time);
}
const baseEpoch = currentDateAsEpoch();
But am getting the error: TypeError: time.split is not a function
I want the baseEpoch to be set as the current date so Google Distance Matrix doesn't return the departure_time error saying time can only be equal to or in the future.
Thank you in advance for your help!
You can use momentjs package for nodejs. This package helps you in dealing date and time effortlessly. You may need to dig more into the docs for better understanding of the moment module(Documentation is simple to understand).
Here is some snippet from momentjs docs.
moment().unix();
//moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
Also using a library like moment which is well tested is better than writing your own functions to handle date and time for following reasons.
The code would be very well tested due to large number of people using them in a day to day basis.
No need to waste your time in reinventing the wheel.
Additional functionalities for future development work.(Like formatting and other date operations)
Easy to understand and implement even for new team members (due very well written documents and good support due to large number of developers using the library)

CRON string incompatability Quartz

I am using Quartz to schedule jobs and display these in an UI. The UI uses an NPM library to calculate the next trigger fire. However, the example string in that library is rejected by Quartz, and a string i have used with success to schedule a result returns invalid fire times from the NPM library.
Quartz Incompatible (works in cron-parser)
*/2 * * * *
CRON-PARSER Incompatible (works in Quartz)
0 0/1 * 1/1 * ? *
Quartz throws Unexpected end of expression exceptions with CRON strings that are valid according to https://crontab.guru/
Can anyone explain why there is an incompatibility here?
Take a look here, there are different cron-expression implementations with Non-standard characters and different number of supported fields in the expression.
So, according to quartz docs, 6 to 7 fields are supported in Quartz, whereas in https://crontab.guru/ only 5 fields are supported.
And in cron-parser, 5 to 6 fields are supported.

Hangfire Cron expressions are not valid

I am using Hangfire and I want to describe different scenarios for my RecurringJobs. But I am not being able to achieve what I am looking for, and if CRON is already limited, the CRON used by Hangfire is yet more.
I went on reading Hangfire documentation and I find a like to https://en.wikipedia.org/wiki/Cron#CRON_expression for more complex expressions then the ones supported by default on Hangfire. But they are not even compatible, for instance, Hangfire only has minutes, hour, month, day, days of the week, but if I use the L or the ? on the day like it says on the documentation it does not work. I have this error the following error for this expression 16 14 L ? ?:
InnerException = {"'L' is not a valid [Day] crontab field value. It must be a numeric value between 1 and 31 (all inclusive)."}
CRON from Hangfire has the following method: Monthly(int day); What happens If I choose for instance 31? It will still run on months like February or April for instance at the last day of each month? Or do I need to do something extra to achieve it?
That way what is happening? I do not seem able to define the condition of the day chosen by the user is 31, to run the background jobs always on the last day of the month. And I don't even talk about days 29 or 30 which are also special causes and which I would use always the last day of the month to process the background job.
I though of using the Month method from Hangfire.CRON but I don't think it will treat the days 29,30 and 31 the way I want.
Do you confirm that Hangfire Cron does not use the Cron expressions that are referenced by documentation and if there is any way to achieve what I am looking for? Also, any suggested tutorial or something to help me out? I have been reading https://github.com/atifaziz/NCrontab which I think it is the one Hangfire uses, but it does not help that much.
You are right about NCrontab. Hangfire uses it, so you should ensure your cron expression is supported by this library. Two simple options to do it:
C# Interactive window (as described in NCrontab Readme, or you could use this example )
Online cron visualizer (like https://crontab.guru or http://cron.schlitt.info)
Cron.Monthly(31) is translated to 0 0 31 * * and job would be triggered only if current month has 31 days.
To run the background job always on the last day of the month, add three separate jobs:
0 0 30 4,6,9,11 *
0 0 31 1,3,5,7,8,10,12 *
0 0 28 2 *
Cron job to run on the last day of the month

Resources