Here we will find number of Sunday between 1st Oct 2020 and 31st Oct 2020. You can put any two dates. Let's go for it.
import java.util.Calendar; public class NumberOfSundays { public static void main(String[] args) { Calendar startDate = Calendar.getInstance(); startDate.set(2020, 9, 1); Calendar endDate = Calendar.getInstance(); endDate.set(2020, 9, 31); int count = 0; while (startDate.before(endDate)) { if (startDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { count++; startDate.add(Calendar.DATE, 7); } else { startDate.add(Calendar.DATE, 1); } } System.out.println(count); } }
0 Comments