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 :


package com.javamultiplex.datetime;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author Rohit Agarwal
 * @category Date and Time Problems
 * @problem Print all mondays of given month and year?
 * 
 */
public class NumberOfMondaysInMonth {

 /*
  * d -> day of month 
  * M -> month of year 
  * y -> year
  */
 private static final String pattern = "dd/MM/yyyy";

 public static void main(String[] args) {

  Scanner input = null;
  try {
   input = new Scanner(System.in);
   System.out.println("Enter month name : ");
   String monthName = input.next();
   String year = null;
   Date date = null;
   String myDate = null;
   List<String> dates = new ArrayList<>();
   DateFormat format = new SimpleDateFormat(pattern);
   int dayAndMonthNumber[] = new int[2];
   int days = 0, monthNumber = 0, mondays = 0;
   // Converting String to uppercase
   monthName = monthName.toUpperCase();
   System.out.println("Enter Year (yyyy) : ");
   year = input.next();
   if (isValidMonth(monthName) && isValidYear(year)) {
    dayAndMonthNumber = getNumberofDaysAndMonthNumberByMonthName(monthName, year);
    days = dayAndMonthNumber[0];
    monthNumber = dayAndMonthNumber[1];
    // Converting String to int.
    int myYear = Integer.parseInt(year);
    // Creating Calendar class instance.
    Calendar cal = Calendar.getInstance();
    // Adding given month and year in Calendar.
    cal.set(Calendar.MONTH, monthNumber);
    cal.set(Calendar.YEAR, myYear);

    /*
     * Adding all the dates related to given month and year to
     * Calendar. and checking day for each date. If it is Monday
     * then increase the counter and print date.
     */
    for (int i = 1; i <= days; i++) {

     // Adding day of month in Calendar.
     cal.set(Calendar.DAY_OF_MONTH, i);
     /*
      * cal.get(Calendar.DAY_OF_WEEK) = 1 (Sunday)
      * cal.get(Calendar.DAY_OF_WEEK) = 2 (Monday)
      * cal.get(Calendar.DAY_OF_WEEK) = 3 (Tuesday)
      * cal.get(Calendar.DAY_OF_WEEK) = 4 (Wednesday)
      * cal.get(Calendar.DAY_OF_WEEK) = 5 (Thursday)
      * cal.get(Calendar.DAY_OF_WEEK) = 6 (Friday)
      * cal.get(Calendar.DAY_OF_WEEK) = 7 (Saturday)
      */
     if (cal.get(Calendar.DAY_OF_WEEK) == 2) {
      date = cal.getTime();
      myDate = format.format(date);
      dates.add(myDate);
      mondays++;

     }
    }
    System.out.println("Number of Mondays : " + mondays);
    System.out.println("Dates are as follows :");
    for (int i = 0; i < mondays; i++) {
     System.out.println(dates.get(i));
    }
   } else {
    System.out.println("Month name should be valid and Year should be 4 digit long.");
   }
  } finally {
   if (input != null) {
    input.close();
   }
  }

 }

 private static int[] getNumberofDaysAndMonthNumberByMonthName(String monthName, String year) {

  int dayAndMonthNumber[] = new int[2];
  int days = 0;
  int monthNumber = 0;
  switch (monthName) {
  case "JANUARY":
  case "JAN":
   days = 31;
   monthNumber = 0;
   break;
  case "FEBRUARY":
  case "FEB":
   if (isLeapYear(year)) {
    days = 29;
   } else {
    days = 28;
   }
   monthNumber = 1;
   break;
  case "MARCH":
  case "MAR":
   days = 31;
   monthNumber = 2;
   break;
  case "APRIL":
  case "APR":
   days = 30;
   monthNumber = 3;
   break;
  case "MAY":
   days = 31;
   monthNumber = 4;
   break;
  case "JUNE":
   days = 30;
   monthNumber = 5;
   break;
  case "JULY":
   days = 31;
   monthNumber = 6;
   break;
  case "AUGUST":
  case "AUG":
   days = 31;
   monthNumber = 7;
   break;
  case "SEPTEMBER":
  case "SEP":
   days = 30;
   monthNumber = 8;
   break;
  case "OCTOBER":
  case "OCT":
   days = 31;
   monthNumber = 9;
   break;
  case "NOVEMBER":
  case "NOV":
   days = 30;
   monthNumber = 10;
   break;
  case "DECEMBER":
  case "DEC":
   days = 31;
   monthNumber = 11;
   break;
  }
  dayAndMonthNumber[0] = days;
  dayAndMonthNumber[1] = monthNumber;
  return dayAndMonthNumber;
 }

 private static boolean isLeapYear(String year) {

  boolean result = false;
  // Converting String to int.
  int myYear = Integer.parseInt(year);
  if ((myYear % 4 == 0 && myYear % 100 != 0) || myYear % 400 == 0) {
   result = true;
  }
  return result;
 }

 private static boolean isValidMonth(String monthName) {

  boolean result = false;
  String[] fullMonthNames = { "JANUARY", "FEBRUARY", "MARCH", "APRIL",
    "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER",
    "NOVEMBER", "DECEMBER" };
  String[] halfMonthNames = { "JAN", "FEB", "MAR", "APR", "AUG", "SEP",
    "OCT", "NOV", "DEC" };
  // Converting Array to List
  List<String> fullMonths = new ArrayList<>(Arrays.asList(fullMonthNames));
  List<String> halfMonths = new ArrayList<>(Arrays.asList(halfMonthNames));
  if (fullMonths.contains(monthName) || halfMonths.contains(monthName)) {
   result = true;
  }
  return result;
 }

 private static boolean isValidYear(String year) {

  boolean result = false;
  // Regular expression that matches a String contains 4 digits.
  String pattern = "[0-9]{4}";
  if (year.matches(pattern)) {
   result = true;
  }
  return result;
 }

}


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.