Java Program to calculate what day of the week you were born based on Date of Birth in Java Using Eclipse IDE
What Will I Learn?
- Application of Calendar class.
- Application of GregorianCalendar Class.
- More String Class methods.
- Application of String return methods.
- More application of switch statements.
Requirements
- An Integrated Development Environment(IDE) installed that can run Java (e.g Eclipse, IntelliJ, NetBeans)
- JDK 1.X installed on your Pc
- Java Programming Experience.
- Of course, willingness to learn
Difficulty
- Intermediate
Tutorial Contents
This application requires the user to input his/her date of birth in this format DD MMM YYYY and then calculates the day when the user was born and also tells the user how old he/she is.
Below are the import statements for the application.
NB: a new Java class file was created called birthDayCalculator.
The main method of the program is below:
Line 9: Creates a scanner object called sc.
Line 10: Creates an integer variable named current year and initializes its value to the current year using the Calendar.getInstance().get(Calendar.YEAR) this gets an instance of the Calendar and gets the value of the current year using the Calendar.YEAR value.
Line 12: Prompts the user to enter his/her date of birth in the format 09 JANUARY/JAN 1990. i.e 09 JAN 2012 or 05 JANUARY 2011.
NB: The user can either enter the value of the month in full or the first three letters i.e JAN or JANUARY.
Line 14: Declares an integer variable called day and initializes its value to the first integer input entered by the user using the .nextInt() method.
Line 15: Declares a String variable called month which takes in the next string entered by the user and uses space as a delimiter which is the default for the .next method of the scanner class.
Line 16: Declares an integer variable called year and initializes its value to the next integer entered by the using the .nextInt() method of the Scanner class.
Line 18: Here an if statement is used to check the value returned from the checkValidity method and sends day, month and year as arguments which were collected in the previous lines (i.e Line 14, 15 and 16).
NB: The checkValidity() method checks to see if the input entered by the user are valid, later in the post the checkValidity method will be explained in details.
Line 19: If the user entries is valid then a String variable called validMonth is assigned the value returned from the formatInput() method that takes a String (i.e the month entered by the user) as its argument.
NB: the formatInput() method returns a valid a String value which is formatted to the accepted month values for the application. The formatInput() method will be explained later in this post.
Line 21: The month variable is then assigned the value gotten from another method called the getNumericalMonth() method which is responsible for returning the numerical value of the month (e.g if the user entered JAN the method returns 0 and December would yield a return value of 11).
NB: The getNumericalMonth() method will be explained later in this post.
Line 23: The realMonth variable is assigned the converted integer value of the month variable (i.e if the month variable contained "0", the realMonth variable would contain 0 as an integer).
Line 25: A GregorianCalender obejct called birthDay is created and in Line 27, the birthDay object is initialized using the year, realMonth and day variables.
Line 29: An integer variable called weekDay is assigned the day of the week in number that the day entered by the user falls on.
Line 31: A String variable called weekday is assigned the value of the integer varibale (weekDay – 1).
Line 33 - 39: Uses the .replace() method of the String class to replace the value in the weekday variable based on its value (i.e 0 will be replaced with Sunday and 6 with Saturday) which is the day the user was born.
Line 41: A String variable called leading is assigned the value returned from the method getLeading() which returns the leading text used to format the output shown to the user based on what day of the month the birth date falls.
(i.e the method returns "th" if the date falls on the 11th , 13th etc ).
NB: the getLeading() method will be explained later in this post.
Line 43: This displays what day the user was born, with the day and then the leading text and the month and the year (e.g You were born on Friday the 12th of November 1990).
Line 44: This displays how old the user is by using the current year variable declared on Line 10 that has the value of the current year and subtracts the entered year and hence the current age of the user is gotten.
Line 46 - 48: Calls a method called displayValidInput() which shows the user the valid inputs accepted by the user. This else statement is only called when the checkValidity() method on Line 18 returns false while Line 48 calls the .close() method on the Scanner object sc.
The explanation of Methods used in the application are below:
FormatInput() Method
The formatInput() method takes in the entered month as entered by the user and its main aim is to convert smaller entry for the month value (i.e JUN,JUL,AUG) to its full form (i.e JUNE,JULY,AUGUST).
Line 144: A String variable called output is assigned the value of the entered month value.
Line 146 - 183: Is a switch statement that checks for certain conditions and then assigns a new value to the output String variable declared on line 144 and then Line 184 returns the value of the output.
For instance; if the entered value is "JAN", output is set to the value "JANUARY" and if FEB, then output is set to the value FEBRUARY.
getLeading() Method:
The getLeading() method that’s in the day entered by the user as an argument and its main aim is to return the end part of the text displayed along with the date when the user was born.
Line 54: A String variable called leading is assigned the value - "th" which will be the default return value except on some certain cases.
Line 56 - 57 : An if statement is used to check if the remainder division of the day by 10 is its equals to 1 and is the integer division of the day by 10 is not 1 and then the leading String variable is given the value of "st".
For instance, say the entered day was 21, 21 % 10 equals 1 and 21 / 10 is 2 and not 1 hence the leading string will have the value "st" and on display the users Date of birth will be rightly formatted as 21st and not 21th.
The same principle applies to the remaining two if statements from line 59 - 64.
Finally, line 65 returns the value of leading.
validateMonth() Method:
The validate month method, takes the year, month and day entered by the user in order to validate that the right month has been entered and that the day entered is not more than is supposed and also that the entered year is not more than the current year as one cannot be born in 2019 while we are still in 2018.
The formula used is September, April, June and November has 30 Days while January, March, May, July, August, October and December all have 31 Days while February has 29 Days in a leap year and 28 when it’s not a leap year.
The method uses a switch statement using the month as the case.
Line 210 - 226 checks to ensure that the entered text is either the name of the month in full or the first three letters in the month.
- Line 223 - 226: An if statement to ensure that the user enters a date not above 31 as earlier stated that some months have the limit of 31 Days.
If the day entered is more than 31, an error message is displayed telling the user that the day entered is more than the available days in the month entered and a false is returned by the user.
Line 229 - 240: Uses the "FEB" and "FEBRUARY" cases to check if the entered month was February and ensures that the day entered is not more than 29 in a leap year and not more than 28 when its not a leap year.
- Line 231 - 240 ensures that the 29 Days when a leap year and 28 Days when not a leap year condition is checked.
If more days than available in February is entered, an error message telling the user what the problem is and a false is returned by the user.
Line 242 - 254: The remaining months which has a total of 30 Days are used as cases here and then the day entered by the user is checked to ensure than not more than 30 was entered by the user.
- Line 250 - 254: returns false from the method and displays an error message telling the user than the entered date is more than the available days in the selected month.
showValidMonthEntry() Method:
The showValidMonthEntry doesn't take any argument and return nothing which is the void return type, it only displays to the user the valid entry for the month if the user enters a wrong value for the month.
getNumericalMonth() Method:
The getNumericalMonth method takes in a String argument and then returns the numerical value of the month. The method returns 0 if the entered month is JANUARY and returns 11 if the entered month is 11.
A switch statement is used to determine what value to return based on the entered value of the month.
displayValidInput() Method:
The displayValidInput() method takes no argument and its main aim is to display to the user the valid entry format accepted by the application and also to show if the user wants the valid month entry which is displayed by calling the showValidMonthEntry() method on line 83 and if not the application exits using the System.exit(1) on line 85, finally line 87 calls the .close() method on the Scanner Object
checkValidity() Method:
This method takes in three arguments which are the day, month and year entered by the user , the main aim of this method is to ensure that the entered values by the user are valid and also return true if the entries are valid and false if otherwise.
Line 190: Creates a integer variable which hold the value of the current year.
Line 192 - 195: An if statement is used to check and ensure that the day entered by the user is not less than 1 and greater than 31 as there exist nothing as day 0 and day 32.
Line 196 - 198: An if statement is also used to ensure that the month entered is valid and this is done by calling the ValidateMonth() method which the month, day and year is sent as an argument to the method.
Line 199 - 202: Uses an if statement to ensure that the entered year is not greater than the current year which is done by using the currentyear integer value declared and assigned on line 190.
Below are code execution:
Valid Code Execution
Invalid Code Execution
The complete code can be located from this GitHub repository: https://github.com/generalkolo/Javacodes/blob/master/src/birthdayDayCalculator.java
Curriculum
- Java Tutorials: If Else Statement
- Understanding Methods (return type - String) in Java using Eclipse IDE
- Understanding SWITCH Statement in Java using Eclipse IDE
Posted on Utopian.io - Rewarding Open Source Contributors
Good post.
This comment has received a 0.17 % upvote from @speedvoter thanks to: @sn0w-fox.
Thanks, @sn0w-fox
Things are not new to me but I always like good written tutorials to pick up new pieces
This comment has received a 0.25 % upvote from @speedvoter thanks to: @sn0w-fox.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks, @cha0s0000.
Funny but useful right?
Alright.. Codes will be added on my next contribution!
Hey @edetebenezer I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x