cron expression for the last weekend of the month - java

i have a job to be run on the last weekend of the month (i.e) if the month has a sunday at the end it should run on sunday and if the month ends or has a saturday at the end the job should run on saturday.
I didnt find any documentation to build a cron expression for this case.
Thanks in advance.

You can use L in the day of week field
0 0 0 ? * SUNL
This will trigger at every last SUN of every month, e.g.
Sunday, August 31, 2014 12:00 AM
Sunday, September 28, 2014 12:00 AM
Sunday, October 26, 2014 12:00 AM
Sunday, November 30, 2014 12:00 AM
Sunday, December 28, 2014 12:00 AM
I don't see a way to specify the last weekend of a month since quartz does not allow to combine the L character with multiple days of week. So you can't do something like SATL,SUNL. If you want to trigger something on every last SAT and SUN I would define 2 cron expressions.
From the quartz documentation (Special characters)
L ("last") - .....
for example "6L" means "the last friday of the month".
I tested it with my cron expression view - a plugin that I wrote for eclipse
https://github.com/link-intersystems/eclipse-plugins-repository. Maybe it is also useful for you,

Related

How do I get Cron Expression over a specific range of dates?

I want to get valid dates between certain dates via cron expression. Is this possible?
Returns only 1 with getNextValidTimeAfter.
For example, my statement is as follows;
0 0 0? MAY, JUN, JUL MON, TUE 2020-2024
This means:
At 00:00:00 am, on every Monday and Tuesday, in May, June and July, between 2020 and 2024
What I want to do is;
To bring the days in this statement between June 2020 and September 2020.
Months are entered as numbers so it should be something like this: 0 0 0 ? 5-9 * 2020-2024. Look at this resource for more information.

Joda Time returning yyyyww for Jan 1, 2016 as 201653

DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyww");
dateTimeFormatter.print(new DateTime(2016, 1, 1, 1, 1).withZone(DateTimeZone.UTC))
returns 201653
Why is it 53 week of 2016 rather than 2015?
What you are looking for is the week-based-year (symbol x), not the year of era (symbol y). See also the pattern syntax used by Joda-Time which deviates from that of SimpleDateFormat or Java-8. So the solution should look like:
DateTimeFormatter f = DateTimeFormat.forPattern("xxxxww");
String s = f.print(new DateTime(2016, 1, 1, 1, 1).withZone(DateTimeZone.UTC));
System.out.println(s); // 201553
Because January 1st was Friday. And in that case, that week counts as last week of 2015, and the first week of 2016 will start on first Monday (January 4th).
That is implemented in accordance with ISO 8601 standard:
There are several mutually equivalent and compatible descriptions of week 01:
- the week with the year's first Thursday in it (the formal ISO definition),
- the week with 4 January in it,
- the first week with the majority (four or more) of its days in the starting year, and
- the week starting with the Monday in the period 29 December – 4 January.

Cron Expression once each 2 days [duplicate]

This question already has answers here:
how to set cronjob for 2 days? [closed]
(3 answers)
Closed 9 years ago.
I need to create a cron expression that fires at a specific time every two days. So, for instance today is 26th of November. If I deploy my web app today and the scheduler should start the 28th the 30th the 2nd of december and so on...
Any help?
Thanks,
Luca
0 0 12 1/2 * ? *
Schedules
1. Wednesday, November 27, 2013 12:00 PM
2. Friday, November 29, 2013 12:00 PM
3. Sunday, December 1, 2013 12:00 PM
4. Tuesday, December 3, 2013 12:00 PM
5. Thursday, December 5, 2013 12:00 PM
For Ref: cronMaker
Quartz cookbook contains the How-To: Trigger That Executes Every 2 Days

Using DateTime to describe a birthday

I'm using the library DateTime to store date values for birthdays.
DateTime dateTime01Abegin = new DateTime(2013, 5, 23, 00, 00);
DateTime dateTime01Bbegin = new DateTime(2012, 5, 22, 00, 00);
Running the method .getDayOfYear() on them, I am getting a value of 143 for both. But one is May 23rd and one is May 22nd - I can't figure why they're returning the same value!
2012 has 366 (february 29) days and 2013 has 365, that's why both dates return 143.
The count of the days in a year has an offset of one day in leap years, since after february 28 leap years have an additional day compared to normal ones.
Not every year is 365 days long, some years are 366 days long.
2012 is a leap year, which means that it has an additional day, February 29th. For dates prior to February 28th, the .getDayOfYear() will return the same values for similar dates for any year. For dates after February 28th, .getDayOfYear() will return the same values for similar dates if both of those dates are in a leap year, or if both of those dates are not in a leap year. Otherwise, they should be off by one.
Leapyears. 2012 is a leapyear, so there was a Feb 29th, pushing all the "later" dates up one slot, so your May 22nd is actually day 143 in both years.
2012 was a leap year. So may 23 2013 came 1 day before may 23 2012.

GWT DateTimeFormat reverses timezone value

Consider the following code is run in GWT:
import com.google.gwt.i18n.client.DateTimeFormat;
...
DateTimeFormat fullDateTimeFormat = DateTimeFormat.getFullDateTimeFormat();
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(-120)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(0)));
Log.info(fullDateTimeFormat.format(date, TimeZone.createTimeZone(180)));
And supposing it is Greenwich time 16:00.
Why do I get the following output?
Monday, February 21, 2011 6:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 1:00:00 PM Etc/GMT+3
The expected one is
Monday, February 21, 2011 2:00:00 PM Etc/GMT-2
Monday, February 21, 2011 4:00:00 PM Etc/GMT
Monday, February 21, 2011 7:00:00 PM Etc/GMT+3
What is the right way to fix it?
"Etc/GMT-2" is actually (and very surprisingly) "+02:00", see http://en.wikipedia.org/wiki/Tz_database#Area:
In order to conform with the POSIX style, those zones beginning with "Etc/GMT" have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign.
Your code leads to a different output on my machine (probably because of my different Locale):
Monday, 2011 February 21 18:00:00 UTC+2
Monday, 2011 February 21 16:00:00 UTC
Monday, 2011 February 21 13:00:00 UTC-3
So, it's not DateTimeFormat which is responsible for the reversing, but TimeZone.createTimeZone(int timeZoneOffsetInMinutes)!
Let's look a bit more into the GWT javadocs of com.google.gwt.i18n.client.TimeZone:
getOffset(Date date):
* Returns the RFC representation of the time zone name for the given date.
* To be consistent with JDK/Javascript API, west of Greenwich will be
* positive.
and composeGMTString(int offset):
* In GMT representation, +/- has reverse sign of time zone offset.
* when offset == 480, it should output GMT-08:00.
I had a look at the source code. It has comments saying
20:00 GMT0000, or 16:00 GMT-0400, or 12:00 GMT-0800
are all the same. As per that I infer the relation between time and timezone as the amount of time subtracted from GMT or added to the GMT. So 16.00 GMT becomes 1400 GMT -0200 or 1900 GMT +0300. Keeping that in mind we have to work the other way around to get the desired result.

Categories

Resources