How to print all the Mondays in given month and year in Java?



Problem Description :


Write a program in Java that prints all the Mondays in given month and year. For example in Jan
2017 there are 5 Mondays and dates are 02/01/2017, 09/01/2017, 16/01/2017, 23/01/2017, 30/01/2017.

Concept :


If you have read our previous date and time problems then believe me this problem would be piece of cake for you. With the help of Calendar class constants MONTH, YEARDAY_OF_MONTH and DAY_OF_WEEK we can easily solve this problem.


Algorithm :


  1. Find number of days in given month.
  2. Set given month and year in Calendar.
  3. Iterate the loop from 1 to number of days ( Step 1 ) times.
  4. Set the day of month as current index in Calendar.
  5. Check the day, if it is Monday print it else go to Step 3.

Recommended :





Java Program :




Output :

Output of java program that prints all the Monday's in given month and year.
Output - Monday's in given month and year in Java

Similar Problems :


1) How to print all the Sundays in given month and year?

  - Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 1) {

2) How to print all the Tuesdays in given month and year?

- Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 3) {

3) How to print all the Wednesdays in given month and year?

- Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 4) {

4) How to print all the Thursdays in given month and year?

- Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 5) {

5) How to print all the Fridays in given month and year?

- Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 6) {

6) How to print all the Saturdays in given month and year?

- Replace line 76 with if (cal.get(Calendar.DAY_OF_WEEK) == 7) {

References :





Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

Tags : Date and Time Problems, DateFormat, SimpleDateFormat, String, Calendar, if else statement, switch statement, List, ArrayList, Leap Year.

About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to print all the Mondays in given month and year in Java? How to print all the Mondays in given month and year in Java? Reviewed by Rohit Agarwal on 3/09/2017 Rating: 5

No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.

Powered by Blogger.