How to find number of days and weeks for given month in Java?



Problem Description :


Write a program in Java that displays number of days and weeks for given month with proper validations.

Concept :


We know that number of days are constant for each month except February because for leap year there are 29 days and for other years 28 days. So in this problem if any user enter month name as February then he has to enter year also for finding number of days. The Solution is very simple and straight forward. We are using switch case for finding number of day and division/remainder mechanism for number of weeks.


Recommended :



Java Program :


package com.javamultiplex.datetime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category Date and Time Problems
* @problem Number of days and weeks per month?
*
*/
public class NumberOfDaysAndWeekPerMonth {
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;
int[] weeksAndRemainingDays = new int[2];
int days = 0;
// Converting String to uppercase
monthName = monthName.toUpperCase();
if (monthName.equals("FEBRUARY") || monthName.equals("FEB")) {
System.out.println("Enter Year : ");
year = input.next();
}
if (isValidMonth(monthName)) {
if ((monthName.equals("FEBRUARY") || monthName.equals("FEB"))) {
if (isValidYear(year)) {
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
System.out.println("Number of days in " + monthName + " " + year + " are : " + days);
weeksAndRemainingDays = getWeeksAndRemainingDays(days);
System.out.println("Number of weeks in " + monthName + " " + year + " are : " + weeksAndRemainingDays[0] + " Weeks + " + weeksAndRemainingDays[1] + " Day/s");
} else {
System.out.println("Year should be 4 digit long.");
}
} else {
days = getNumberofDaysByMonthName(monthName);
System.out.println("Number of days in " + monthName + " are : " + days);
weeksAndRemainingDays = getWeeksAndRemainingDays(days);
System.out.println("Number of weeks in " + monthName + " are : " + weeksAndRemainingDays[0] + " Weeks + " + weeksAndRemainingDays[1] + " Day/s");
}
} else {
System.out.println("Month name is not valid.");
}
} finally {
if (input != null) {
input.close();
}
}
}
private static int[] getWeeksAndRemainingDays(int days) {
int weeks[] = new int[2];
weeks[0] = days / 7;
weeks[1] = days % 7;
return weeks;
}
private static int getNumberofDaysByMonthName(String monthName) {
int days = 0;
switch (monthName) {
case "JANUARY":
case "JAN":
days = 31;
break;
case "MARCH":
case "MAR":
days = 31;
break;
case "APRIL":
case "APR":
days = 30;
break;
case "MAY":
days = 31;
break;
case "JUNE":
days = 30;
break;
case "JULY":
days = 31;
break;
case "AUGUST":
case "AUG":
days = 31;
break;
case "SEPTEMBER":
case "SEP":
days = 30;
break;
case "OCTOBER":
case "OCT":
days = 31;
break;
case "NOVEMBER":
case "NOV":
days = 30;
break;
case "DECEMBER":
case "DEC":
days = 31;
break;
}
return days;
}
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 :


Case 1 :


Output of Java program that finds number of days and weeks for given month - case1
Output - Other than Feb month 

Case 2 :


Output of Java program that finds number of days and weeks for given month - case2
Output - Feb month

References :


https://en.wikipedia.org/wiki/Regular_expression

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

https://docs.oracle.com/javase/7/docs/api/java/util/List.html

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

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, Days and weeks for given month, Solution in Java, String, if else statement, List, ArrayList, Regular expressions, switch statement, 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 find number of days and weeks for given month in Java? How to find number of days and weeks for given month in Java? Reviewed by Rohit Agarwal on 1/20/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.