Java || Display Today’s Date Using a Switch Statement
If statements, char’s and strings have been previously discussed, and this page will be more of the same. This program will demonstrate how to use a switch statement to display today’s date, converting from mm/dd/yyyy format (i.e 6/17/12) to formal format (i.e June 17th, 2012).
This same program can easily be done using if statements, but sometimes that is not always the fastest programming method. Switch statements are like literal light switches because the code “goes down a line” to check to see which case is valid or not, just like if statements. You will see why switches are very effective when used right by examining this program.
====== TODAY’S DATE USING A SWITCH ======
So to start our program out, lets define the variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Scanner; public class TodaysDate { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables int month = 6; int day = 17; int year = 2012; }// end of main }// http://programmingnotes.org/ |
Notice on line 6 there is a variable named “cin.” This program uses the scanner class to obtain data from the user. Click here for various examples demonstrating how to obtain data from the user using the scanner class.
We also declared three other variables, named “month, day, and year.” You should always name your variables something descriptive, as well as initializing them to a starting value.
Next we get data from the user for the month, day, and year variables. This process is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 |
// get month from user System.out.print("Please enter the current month: "); month = cin.nextInt(); // get day from user System.out.print("Please enter the current day: "); day = cin.nextInt(); // get year from user System.out.print("Please enter the current year: "); year = cin.nextInt(); |
Notice the format that the user will input the data in. They will input data in mm/dd/yyyy format, and using the “cin” variable will make that possible.
So after obtaining data from the user, how will the program convert numbers into actual text? Next comes the switch statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// determine which month to display switch(month) { case 1: System.out.print("tJanuary "); break; case 2: System.out.print("tFebruary "); break; case 3: System.out.print("tMarch "); break; case 4: System.out.print("tApril "); break; case 5: System.out.print("tMay "); break; case 6: System.out.print("tJune "); break; case 7: System.out.print("tJuly "); break; case 8: System.out.print("tAugust "); break; case 9: System.out.print("tSeptember "); break; case 10: System.out.print("tOctober "); break; case 11: System.out.print("tNovember "); break; case 12: System.out.print("tDecember "); break; default: System.out.print("t" + month +" is not a valid monthn"); System.exit(1); break; }// http://programmingnotes.org/ |
Line 2 contains the switch declaration, and its comparing the variable of “month” to the 12 different cases that is defined within the switch statement. So this piece of code will “go down the line” comparing to see if the user obtained data is any of the numbers from 1 to 12, as defined in the switch statement. If the user chooses a number which does not fall between 1 thru 12, the “default” case will be executed, prompting the user that the data they entered was invalid, which can be seen in line 40. Notice line 42 has an exit code. This program will force an exit whenever the user enters invalid data.
Line 6 is also very important, because that forces the computer to “break” away from the selected case whenever it is done examining that specific piece of code. It is important to add the break statement in there to avoid errors, which may result if the program does not break away from the current statement in which it is examining. Try compiling this code removing the “break” statements and see what happens!
Next we will add another switch statement in order to convert the day of the month to have a number suffix (i.e displaying the number in 1st, 2nd, 3rd format). This is very similar to the previous switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// determine which suffix to display switch(day) { case 1: case 21: case 31: System.out.print(day + "st, "); break; case 2: case 22: System.out.print(day + "nd, "); break; case 3: case 23: System.out.print(day + "rd, "); break; default: System.out.print(day + "th, "); break; } |
This block of code is very similar to the previous one. Line 2 is declaring the variable ‘day’ to be compared with the base cases; line 6 and so forth has the break lines, but line 4, 7 and 10 are different. If you notice, line 4, 7 and 10 are comparing multiple cases in one line. Yes, with switch statements, you can do that. Just like you can compare multiple values in if statements, the same can be done here. So this switch is comparing the number the user entered into the program, with the base cases, adding a suffix to the end of the number.
So far we have obtained data from the user, compared the month and day using switch statements and displayed that to the screen. Now all we have to do is output the year to the user. This is fairly simple, because the year is not being compared, you are just simply using a system print to display data to the user.
1 2 |
// display the current year System.out.println(year); |
So finally, adding all the above snippets together should give us the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import java.util.Scanner; public class TodaysDate { // global variable declaration static Scanner cin = new Scanner(System.in); public static void main(String[] args) { // declare variables int month = 6; int day = 17; int year = 2012; // display message to screen System.out.println("nWelcome to My Programming Notes' Java Program."); // get month from user System.out.print("nPlease enter the current month: "); month = cin.nextInt(); // get day from user System.out.print("Please enter the current day: "); day = cin.nextInt(); // get year from user System.out.print("Please enter the current year: "); year = cin.nextInt(); System.out.println("nTodays date is: "); // use this switch statement to display the current month // using the information obtained from above switch(month) { case 1: System.out.print("tJanuary "); break; case 2: System.out.print("tFebruary "); break; case 3: System.out.print("tMarch "); break; case 4: System.out.print("tApril "); break; case 5: System.out.print("tMay "); break; case 6: System.out.print("tJune "); break; case 7: System.out.print("tJuly "); break; case 8: System.out.print("tAugust "); break; case 9: System.out.print("tSeptember "); break; case 10: System.out.print("tOctober "); break; case 11: System.out.print("tNovember "); break; case 12: System.out.print("tDecember "); break; default: System.out.print("t" + month +" is not a valid monthn"); System.exit(1); break; } // use this switch statement to display the current day // using the information obtained from above switch(day) { case 1: case 21: case 31: System.out.print(day + "st, "); break; case 2: case 22: System.out.print(day + "nd, "); break; case 3: case 23: System.out.print(day + "rd, "); break; default: System.out.print(day + "th, "); break; } // display the current year System.out.println(year); }// end of main }// http://programmingnotes.org/ |
Once compiled, you should get this as your output:
Welcome to My Programming Notes' Java Program.
Please enter the current month: 7
Please enter the current day: 28
Please enter the current year: 2012Todays date is:
July 28th, 2012
Leave a Reply