Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
My requirement is I need to find dates for 2nd Sunday of March and first Sunday of November for a particular year and then compare it with my batch date.
For ex - batch date is 2021-06-02
Then I need to find date for 2nd Sunday of March for 2021 and compare it with my batch date. Year I need to extract from batch date and find according to that year.
Can anyone help me with this.
so you can do this method:
public static Date countWeekendDays(int year, int month,int pos) {
Date[] sundays = new Date[6];
Date ret;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY) {
sundays[count]= calendar.getTime();
count++;
}
}
ret = sundays[pos-1];
return ret;
}
then you call it :
System.out.println(Test_1.countWeekendDays(2021,3,2));
System.out.println(Test_1.countWeekendDays(2021,11,1));
System.out.println(Test_1.countWeekendDays(2021,8,1));
and here the result :
Sun Mar 14 11:21:37 CET 2021
Sun Nov 07 11:21:37 CET 2021
Sun Aug 01 11:21:37 CEST 2021
Hi @Anu Kumar you can create a routine and put this code in :
import java.util.Calendar;
import java.util.Date;
public class Test {
public static final Date getSecondSundayOfMarch (Integer year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.MARCH);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.set(Calendar.WEEK_OF_MONTH,2);
return cal.getTime();
}
public static final Date getFirstSundayOfNovember (Integer year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Calendar.NOVEMBER);
cal.setFirstDayOfWeek(Calendar.SUNDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.set(Calendar.WEEK_OF_MONTH,1);
return cal.getTime();
}
}
then you can call them in your job like this :
Test.getFirstSundayOfNovember (2021) for example
Send me Love and Kudos
Hi @guenneguez jeremy. This solution is not giving correct dates as expected.
While passing 2021, it is giving
31 Oct 2021 and 7 Mar 2021.
Both are wrong
ok i get it , just delete the line
cal.setFirstDayOfWeek(Calendar.SUNDAY);
in each method then it will return :
Sun Nov 07 10:51:42 CET 2021
Sun Mar 14 10:51:42 CET 2021
as far is see, it depends of wich day you want to begin a week , if you choose sunday
for the first week of march it will take the last day of february as the first sunday.
so it will send you march 7 as the second sunday wich is wrong , i will see if i can get all the sunday of a month and get first or second in the array
so you can do this method:
public static Date countWeekendDays(int year, int month,int pos) {
Date[] sundays = new Date[6];
Date ret;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY) {
sundays[count]= calendar.getTime();
count++;
}
}
ret = sundays[pos-1];
return ret;
}
then you call it :
System.out.println(Test_1.countWeekendDays(2021,3,2));
System.out.println(Test_1.countWeekendDays(2021,11,1));
System.out.println(Test_1.countWeekendDays(2021,8,1));
and here the result :
Sun Mar 14 11:21:37 CET 2021
Sun Nov 07 11:21:37 CET 2021
Sun Aug 01 11:21:37 CEST 2021
Ok thanks @guenneguez jeremy
Can I get only the dates here like 2021-03-14 as I have to compare it with batch date which is in same format mentioned above in string.
just set "YYYY-MM-dd" in the output schema date pattern of your component where you call the method , it will format the date in the format you want