clanecollege

What Day solution

May 16th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class WhatDay
  6. {
  7.     public static void printDay() throws NumberFormatException, IOException
  8.     {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(
  10.                 System.in));
  11.         System.out.println("Enter the day, (1...31) -->");
  12.         int currentDay = Integer.parseInt(reader.readLine());
  13.         System.out.println("Enter the month, (1...12) -->");
  14.         int currentMonth = Integer.parseInt(reader.readLine());
  15.         System.out.println("Enter the year -->");
  16.         int currentYear = Integer.parseInt(reader.readLine());
  17.  
  18.         // is input year a leap year?
  19.         int leapValue = 0;
  20.         if (isLeapYear(currentYear) == 1
  21.                 && (currentMonth == 1 || currentMonth == 2))
  22.         {
  23.             leapValue = 1;
  24.         }
  25.         else
  26.         {
  27.             leapValue = 0;
  28.         }
  29.         int amtDaysCalc = (currentYear - 1900) + ((currentYear - 1900) / 4)
  30.                 - leapValue + amtDaysUpToCurrentDate(currentDay, currentMonth);
  31.         int dayNumVar = amtDaysCalc % 7;
  32.         String day = findDayFromNum(dayNumVar);
  33.         System.out.println("Day is " + day);
  34.  
  35.     }
  36.  
  37.     public static int isLeapYear(int year)
  38.     {
  39.         int leap;
  40.         if (year % 400 == 0 || year % 100 == 0 || year % 4 == 0)
  41.         {
  42.             leap = 1;
  43.         }
  44.         else
  45.         {
  46.             leap = 0;
  47.         }
  48.         return leap;
  49.     }
  50.  
  51.     public static int amtDaysUpToCurrentDate(int day, int month)
  52.     {
  53.         int result = 0, tally = 0;
  54.         final int amtDaysJan = 31, amtDaysFeb = 28, amtDaysMar = 31, amtDaysApr = 30, amtDaysMay = 31, amtDaysJun = 30, amtDaysJul = 31, amtDaysAug = 31, amtDaysSep = 30, amtDaysOct = 31, amtDaysNov = 30, amtDaysDec = 31;
  55.         int[] days = { amtDaysJan, amtDaysFeb, amtDaysMar, amtDaysApr,
  56.                 amtDaysMay, amtDaysJun, amtDaysJul, amtDaysAug, amtDaysSep,
  57.                 amtDaysOct, amtDaysNov, amtDaysDec };
  58.  
  59.         if (month == 1)
  60.         {
  61.             result = day;
  62.             return result;
  63.         }
  64.         else if (month > 1 && month <= 12)
  65.         {
  66.             for (int i = 0; i < month - 1; i++)
  67.             {
  68.                 tally += days[i];
  69.             }
  70.         }
  71.         else
  72.         {
  73.             System.err.println("Error, invalid month.");
  74.         }
  75.         return result = tally + day;
  76.     }
  77.  
  78.     public static String findDayFromNum(int num)
  79.     {
  80.         if (num < 0 || num > 6)
  81.         {
  82.             num = 7;
  83.         }
  84.         String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday",
  85.                 "Thursday", "Friday", "Saturday", "Error" };
  86.         return days[num];
  87.     }
  88.  
  89.     public static void main(String[] args) throws NumberFormatException,
  90.             IOException
  91.     {
  92.         printDay();
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment