Skip to content

Instantly share code, notes, and snippets.

@henooxx5678
Last active August 29, 2015 14:02
Show Gist options
  • Save henooxx5678/4fda0ff76b9c8fcbe88c to your computer and use it in GitHub Desktop.
Save henooxx5678/4fda0ff76b9c8fcbe88c to your computer and use it in GitHub Desktop.
package findDay;
public class MainClass {
private final static int YEAR = 2100;
private final static int MONTH = 12;
private final static int DATE = 31;
private final static Date INPUT = new Date(YEAR, MONTH, DATE);
private final static Date STANDARD = new Date(2012, 1, 1);
private final static int STD_DAY = 0;
public static void main (String args[]) {
int differenceY = INPUT.getYear() - STANDARD.getYear();
int leapDays = countLeapDays(differenceY, INPUT.getMonth());
int normalDifferenceDays = countNormalDaysDifference(INPUT.getMonth(), INPUT.getDate());
int monthExtraDays = countMonthExtraDays(INPUT.getMonth());
int differenceDays = (differenceY * 365) + normalDifferenceDays + monthExtraDays + leapDays;
int result = STD_DAY + differenceDays % 7;
System.out.println(result);
}
private static int countLeapDays(int differenceY, int month) {
int counter = 0;
for (int i = 0; i < differenceY; i++) {
if (isLeapYear(STANDARD.getYear() + i)) {
counter++;
}
}
if (isLeapYear(STANDARD.getYear() + differenceY) && month > 2) {
counter++;
}
return counter;
}
private static int countNormalDaysDifference(int month, int date) {
return (month - STANDARD.getMonth()) * 30 + (date - STANDARD.getDate());
}
private static int countMonthExtraDays(int month) {
int counter = 0;
for (int i = STANDARD.getMonth(); i < month; i++) {
if (i <= 7 && i % 2 == 1 || i >= 8 && i % 2 == 0) {
counter++;
}else if (i == 2) {
counter -= 2;
}
}
return counter;
}
private static boolean isLeapYear(int year) {
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment