Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- import java.lang.Math;
- /*Write a program which takes a date in day/month/year format (e.g. 25/11/2015) and, if the date is valid,
- *then it presents the date in, for example, “Wednesday, 25th November 2015” format.
- *You must include the following functions:
- *numberEnding() which takes a day number returns “th”, “st”, “nd” or “rd”.
- *monthName() which takes a month number (1-12) and return “January”, or “February”, …
- *dayOfTheWeek() which takes a day, month and year and returns “Monday”, or “Tuesday”, …
- *The day of the week function should use the Gaussian algorithm…
- *w = (day + floor(2.6 * (((month + 9) % 12) + 1) - 0.2) + y + floor(y/4) + floor(c/4) - 2c) mod 7
- *where
- *Y: year-1 for January or February,
- *year for the rest of the year
- *y: last 2 digits of
- *c: first 2 digits of Y
- *w: day of week (0=Sunday,..6=Saturday)
- *mod 7 needs to return a positive number (even if the
- *passed value is negative.
- */
- public class DayOfTheWeek {
- public static final int DAYS_IN_APRIL_JUNE_SEPT_NOV = 30;
- public static final int DAYS_IN_FEBRUARY_NORMALLY = 28;
- public static final int DAYS_IN_FEBRUARY_IN_LEAP_YEAR = 29;
- public static final int DAYS_IN_MOST_MONTHS = 31;
- public static final int NUMBER_OF_MONTHS = 12;
- public static void main(String[] args) {
- try
- {
- String input = JOptionPane.showInputDialog("Enter date (day/month/year):");
- Scanner scanner = new Scanner( input );
- scanner.useDelimiter("/");
- int day = scanner.nextInt();
- int month = scanner.nextInt();
- int year = scanner.nextInt();
- if (validDate( day, month, year))
- {
- JOptionPane.showMessageDialog(null, (dayOfTheWeek(day, month, year)) + ", " + day + (numberEnding(day)) + " " + (monthName(month)) + " " + year);
- }
- else
- {
- JOptionPane.showMessageDialog(null, "" + day + "/" + month + "/" + year + " is not a valid date.",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- scanner.close();
- }
- catch (NullPointerException exception)
- {
- }
- catch (java.util.NoSuchElementException exception)
- {
- JOptionPane.showMessageDialog(null, "No number entered.",
- "Error", JOptionPane.ERROR_MESSAGE);
- }
- }
- public static String numberEnding (int day)
- {
- String numberEnding = "";
- if (day >= 11 && day <= 13)
- {
- numberEnding = "th";
- return (numberEnding);
- }
- // int tenRemainder = day % 10;
- switch (day % 10)
- {
- case 1:
- numberEnding = "st";
- break;
- case 2:
- numberEnding = "nd";
- break;
- case 3:
- numberEnding = "rd";
- break;
- default:
- numberEnding = "th";
- break;
- }
- return (numberEnding);
- }
- public static String monthName (int month)
- {
- String monthName = "";
- switch(month)
- {
- case 1:
- monthName = "January";
- break;
- case 2 :
- monthName = "February";
- break;
- case 3 :
- monthName = "March";
- break;
- case 4 :
- monthName = "April";
- break;
- case 5 :
- monthName = "May";
- break;
- case 6 :
- monthName = "June";
- break;
- case 7 :
- monthName = "July";
- break;
- case 8 :
- monthName = "August";
- break;
- case 9 :
- monthName = "September";
- break;
- case 10 :
- monthName = "October";
- break;
- case 11 :
- monthName = "November";
- break;
- case 12 :
- monthName = "December";
- break;
- }
- return (monthName);
- }
- public static String dayOfTheWeek (int day, int month, int year)
- {
- /* y = (year-1) for January or February or (year) for every other month (last two digits of the year)
- * y = last two digits of the year
- * c = first 2 digits of year
- */
- int c = year / 100;
- int y = year % 100;
- if (month == 1 | month == 2)
- {
- y = y - 1;
- }
- int dayOfTheWeekValue = ((int)(day + Math.floor(2.6 * (((month + 9) % 12) + 1) - 0.2) + y + Math.floor(y/4) +
- Math.floor(c/4) - 2*c) % 7);
- String dayOfTheWeek = "";
- switch(dayOfTheWeekValue)
- {
- case 1:
- dayOfTheWeek = "Monday";
- break;
- case 2:
- dayOfTheWeek = "Tuesday";
- break;
- case 3:
- dayOfTheWeek = "Wednesday";
- break;
- case 4:
- dayOfTheWeek = "Thursday";
- break;
- case 5:
- dayOfTheWeek = "Friday";
- break;
- case 6:
- dayOfTheWeek = "Saturday";
- break;
- default:
- dayOfTheWeek = "Sunday";
- break;
- }
- return (dayOfTheWeek);
- }
- public static boolean validDate( int day, int month, int year)
- {
- return ((year >= 0) && (month >= 1) && (month <= NUMBER_OF_MONTHS) &&
- (day >= 1) && (day <= daysInMonth( month, year )));
- }
- public static int daysInMonth( int month, int year )
- {
- int daysInMonth = DAYS_IN_MOST_MONTHS;
- switch (month)
- {
- case 2:
- daysInMonth = isLeapYear( year ) ? DAYS_IN_FEBRUARY_IN_LEAP_YEAR :
- DAYS_IN_FEBRUARY_NORMALLY;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- daysInMonth = DAYS_IN_APRIL_JUNE_SEPT_NOV;
- break;
- default:
- daysInMonth = DAYS_IN_MOST_MONTHS;
- break;
- }
- return (daysInMonth);
- }
- public static boolean isLeapYear( int year )
- {
- return (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement