Advertisement
myersjo

DayOfTheWeek2

Nov 27th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. import java.util.Scanner;
  2. import javax.swing.JOptionPane;
  3. import java.lang.Math;
  4. /*Write a program which takes a date in day/month/year format (e.g. 25/11/2015) and, if the date is valid,
  5.  *then it presents the date in, for example, “Wednesday, 25th November 2015” format.
  6.  *You must include the following functions:
  7.  *numberEnding() which takes a day number returns “th”, “st”, “nd” or “rd”.
  8.  *monthName() which takes a month number (1-12) and return “January”, or “February”, …
  9.  *dayOfTheWeek() which takes a day, month and year and returns “Monday”, or “Tuesday”, …
  10.  *The day of the week function should use the Gaussian algorithm…
  11.  *w = (day + floor(2.6 * (((month + 9) % 12) + 1) - 0.2) + y + floor(y/4) + floor(c/4) - 2c) mod 7
  12.  *where
  13.  *Y: year-1 for January or February,
  14.  *year for the rest of the year
  15.  *y: last 2 digits of
  16.  *c: first 2 digits of Y
  17.  *w: day of week (0=Sunday,..6=Saturday)
  18.  *mod 7 needs to return a positive number (even if the
  19.  *passed value is negative.
  20.  */
  21.  
  22. public class DayOfTheWeek {
  23.  
  24.         public static final int DAYS_IN_APRIL_JUNE_SEPT_NOV = 30;
  25.         public static final int DAYS_IN_FEBRUARY_NORMALLY = 28;
  26.         public static final int DAYS_IN_FEBRUARY_IN_LEAP_YEAR = 29;
  27.         public static final int DAYS_IN_MOST_MONTHS = 31;
  28.         public static final int NUMBER_OF_MONTHS = 12;
  29.        
  30.         public static void main(String[] args) {
  31.        
  32.                 try
  33.                 {
  34.                         String input = JOptionPane.showInputDialog("Enter date (day/month/year):");
  35.                         Scanner scanner = new Scanner( input );
  36.                         scanner.useDelimiter("/");
  37.                         int day = scanner.nextInt();
  38.                         int month = scanner.nextInt();
  39.                         int year = scanner.nextInt();
  40.  
  41.                         if (validDate( day, month, year))
  42.                         {
  43.                                 JOptionPane.showMessageDialog(null, (dayOfTheWeek(day, month, year)) + ", " + day + (numberEnding(day)) + " " + (monthName(month)) + " " + year);
  44.                         }
  45.                         else
  46.                         {
  47.                                 JOptionPane.showMessageDialog(null, "" + day + "/" + month + "/" + year + " is not a valid date.",
  48.                                                 "Error", JOptionPane.ERROR_MESSAGE);
  49.                         }
  50.                         scanner.close();
  51.                 }
  52.                 catch (NullPointerException exception)
  53.                 {
  54.                 }
  55.                 catch (java.util.NoSuchElementException exception)
  56.                 {
  57.                         JOptionPane.showMessageDialog(null, "No number entered.",
  58.                                         "Error", JOptionPane.ERROR_MESSAGE);
  59.                 }
  60.                
  61.         }
  62.        
  63.         public static String numberEnding (int day)
  64.         {
  65.                 String numberEnding = "";
  66.                 if (day >= 11 && day <= 13)
  67.                 {
  68.                     numberEnding = "th";
  69.                     return (numberEnding);
  70.                 }
  71.                // int tenRemainder = day % 10;
  72.                 switch (day % 10)
  73.                 {
  74.                 case 1:
  75.                     numberEnding = "st";
  76.                     break;
  77.                 case 2:
  78.                     numberEnding = "nd";
  79.                     break;
  80.                 case 3:
  81.                     numberEnding = "rd";
  82.                     break;
  83.                 default:
  84.                     numberEnding = "th";
  85.                     break;
  86.                 }
  87.                 return (numberEnding);
  88.         }
  89.        
  90.         public static String monthName (int month)
  91.         {
  92.             String monthName = "";
  93.             switch(month)
  94.             {
  95.             case 1:
  96.                 monthName = "January";
  97.                 break;
  98.             case 2 :
  99.                 monthName = "February";
  100.                 break;
  101.             case 3 :
  102.                 monthName = "March";
  103.                 break;
  104.             case 4 :
  105.                 monthName = "April";
  106.                 break;
  107.             case 5 :
  108.                 monthName = "May";
  109.                 break;
  110.             case 6 :
  111.                 monthName = "June";
  112.                 break;
  113.             case 7 :
  114.                 monthName = "July";
  115.                 break;
  116.             case 8 :
  117.                 monthName = "August";
  118.                 break;
  119.             case 9 :
  120.                 monthName = "September";
  121.                 break;
  122.             case 10 :
  123.                 monthName = "October";
  124.                 break;
  125.             case 11 :
  126.                 monthName = "November";
  127.                 break;
  128.             case 12 :
  129.                 monthName = "December";
  130.                 break;
  131.             }
  132.             return (monthName);
  133.         }
  134.        
  135.         public static String dayOfTheWeek (int day, int month, int year)
  136.         {
  137.             /* y = (year-1) for January or February or (year) for every other month (last two digits of the year)
  138.              * y = last two digits of the year
  139.              * c = first 2 digits of year
  140.              */
  141.             int c = year / 100;
  142.             int y = year % 100;
  143.             if (month == 1 | month == 2)
  144.             {
  145.                 y = y - 1;
  146.             }
  147.             int dayOfTheWeekValue = ((int)(day + Math.floor(2.6 * (((month + 9) % 12) + 1) - 0.2) + y + Math.floor(y/4) +
  148.                     Math.floor(c/4) - 2*c) % 7);
  149.             String dayOfTheWeek = "";
  150.             switch(dayOfTheWeekValue)
  151.             {
  152.             case 1:                
  153.                 dayOfTheWeek = "Monday";
  154.                 break;
  155.             case 2:
  156.                 dayOfTheWeek = "Tuesday";
  157.                 break;
  158.             case 3:
  159.                 dayOfTheWeek = "Wednesday";
  160.                 break;
  161.             case 4:
  162.                 dayOfTheWeek = "Thursday";
  163.                 break;
  164.             case 5:
  165.                 dayOfTheWeek = "Friday";
  166.                 break;
  167.             case 6:
  168.                 dayOfTheWeek = "Saturday";
  169.                 break;
  170.             default:
  171.                 dayOfTheWeek = "Sunday";
  172.                 break;
  173.             }
  174.             return (dayOfTheWeek);
  175.         }
  176.        
  177.         public static boolean validDate( int day, int month, int year)
  178.         {
  179.                 return ((year >= 0) && (month >= 1) && (month <= NUMBER_OF_MONTHS) &&
  180.                                 (day >= 1) && (day <= daysInMonth( month, year )));
  181.         }
  182.        
  183.         public static int daysInMonth( int month, int year )
  184.         {
  185.                 int daysInMonth = DAYS_IN_MOST_MONTHS;
  186.                 switch (month)
  187.                 {
  188.                 case 2:
  189.                         daysInMonth = isLeapYear( year ) ? DAYS_IN_FEBRUARY_IN_LEAP_YEAR :
  190.                                                                        DAYS_IN_FEBRUARY_NORMALLY;
  191.                         break;
  192.                 case 4:
  193.                 case 6:
  194.                 case 9:
  195.                 case 11:
  196.                         daysInMonth = DAYS_IN_APRIL_JUNE_SEPT_NOV;
  197.                         break;
  198.                 default:
  199.                         daysInMonth = DAYS_IN_MOST_MONTHS;
  200.                         break;
  201.                 }
  202.                 return (daysInMonth);
  203.         }
  204.        
  205.         public static boolean isLeapYear( int year )
  206.         {
  207.                 return (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0));
  208.         }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement