Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class WhatDay
- {
- public static void printDay() throws NumberFormatException, IOException
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));
- System.out.println("Enter the day, (1...31) -->");
- int currentDay = Integer.parseInt(reader.readLine());
- System.out.println("Enter the month, (1...12) -->");
- int currentMonth = Integer.parseInt(reader.readLine());
- System.out.println("Enter the year -->");
- int currentYear = Integer.parseInt(reader.readLine());
- // is input year a leap year?
- int leapValue = 0;
- if (isLeapYear(currentYear) == 1
- && (currentMonth == 1 || currentMonth == 2))
- {
- leapValue = 1;
- }
- else
- {
- leapValue = 0;
- }
- int amtDaysCalc = (currentYear - 1900) + ((currentYear - 1900) / 4)
- - leapValue + amtDaysUpToCurrentDate(currentDay, currentMonth);
- int dayNumVar = amtDaysCalc % 7;
- String day = findDayFromNum(dayNumVar);
- System.out.println("Day is " + day);
- }
- public static int isLeapYear(int year)
- {
- int leap;
- if (year % 400 == 0 || year % 100 == 0 || year % 4 == 0)
- {
- leap = 1;
- }
- else
- {
- leap = 0;
- }
- return leap;
- }
- public static int amtDaysUpToCurrentDate(int day, int month)
- {
- int result = 0, tally = 0;
- 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;
- int[] days = { amtDaysJan, amtDaysFeb, amtDaysMar, amtDaysApr,
- amtDaysMay, amtDaysJun, amtDaysJul, amtDaysAug, amtDaysSep,
- amtDaysOct, amtDaysNov, amtDaysDec };
- if (month == 1)
- {
- result = day;
- return result;
- }
- else if (month > 1 && month <= 12)
- {
- for (int i = 0; i < month - 1; i++)
- {
- tally += days[i];
- }
- }
- else
- {
- System.err.println("Error, invalid month.");
- }
- return result = tally + day;
- }
- public static String findDayFromNum(int num)
- {
- if (num < 0 || num > 6)
- {
- num = 7;
- }
- String[] days = { "Sunday", "Monday", "Tuesday", "Wednesday",
- "Thursday", "Friday", "Saturday", "Error" };
- return days[num];
- }
- public static void main(String[] args) throws NumberFormatException,
- IOException
- {
- printDay();
- }
- }
Add Comment
Please, Sign In to add comment