|
1 | 1 | public class Calendar { |
2 | | - public static void main(String[] args) { |
3 | | - |
4 | | - } |
| 2 | + |
| 3 | + public static boolean Leap(int n) { |
| 4 | + if ((n % 4 == 0 && n % 100 != 0) || (n % 400 == 0)) |
| 5 | + return true; |
| 6 | + return false; |
| 7 | + } |
| 8 | + |
| 9 | + public static int tempDate(int month, int year) { |
| 10 | + if (month == 4 || month == 6 || month == 8 || month == 10 || month == 12) |
| 11 | + return month; |
| 12 | + else if (month == 1) |
| 13 | + return Leap(year) ? 4 : 3; |
| 14 | + else if (month == 3) |
| 15 | + return 7; |
| 16 | + else if (month == 9 || month == 5) |
| 17 | + return month == 9 ? 5 : 9; |
| 18 | + else if (month == 11 || month == 7) |
| 19 | + return month == 7 ? 11 : 7; |
| 20 | + return 0; |
| 21 | + } |
| 22 | + |
| 23 | + public static int yearOldDays(int year) { |
| 24 | + int tensYear = year % 100; |
| 25 | + year -= tensYear; |
| 26 | + int sum = 0; |
| 27 | + int yearOldays[] = { 5, 3, 1, 0 }; |
| 28 | + year = (year % 400) / 100; |
| 29 | + // System.out.println(year); |
| 30 | + if(year!=0) |
| 31 | + sum += yearOldays[year - 1]; |
| 32 | + // System.out.println(sum); |
| 33 | + int leapYearInTensYear = (int) tensYear / 4; |
| 34 | + int nonleapYearInTensYear = tensYear - leapYearInTensYear; |
| 35 | + // System.out.println(((leapYearInTensYear * 2) + nonleapYearInTensYear)%7); |
| 36 | + sum += ((leapYearInTensYear * 2) + nonleapYearInTensYear) % 7; |
| 37 | + // System.out.println(sum); |
| 38 | + return sum; |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public static int findFebLastDay(int year) { |
| 43 | + int sum = Leap(year) ? 4 : 3; |
| 44 | + // System.out.println(sum+yearOldDays(year-1)); |
| 45 | + return sum + yearOldDays(year-1); |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + String day[] = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" }; |
| 50 | + int date = 24, month = 04, year = 2002; |
| 51 | + int febLastDay = findFebLastDay(year); |
| 52 | + int tempDate = tempDate(month, year), tempDay = febLastDay; |
| 53 | + if (tempDate == date) |
| 54 | + System.out.println(day[tempDay]); |
| 55 | + else { |
| 56 | + int dayDifference = date - tempDate; |
| 57 | + if(date>tempDate) { |
| 58 | + tempDay = (tempDay+dayDifference) % 7; |
| 59 | + } else { |
| 60 | + tempDay = tempDay+dayDifference; |
| 61 | + if(tempDay<0) { |
| 62 | + tempDay+=7; |
| 63 | + } |
| 64 | + } |
| 65 | + // System.out.println(tempDay); |
| 66 | + System.out.println(day[tempDay]); |
| 67 | + } |
| 68 | + } |
5 | 69 | } |
6 | 70 |
|
7 | 71 |
|
@@ -31,8 +95,9 @@ public static void main(String[] args) { |
31 | 95 | 300 - 1 days |
32 | 96 | 400 - 0 days |
33 | 97 |
|
34 | | -Feb last day = (date/month) ==> 4/4, 6/6, 8/8,10/10, |
35 | | - 12/12,9/5,11/7,7/11,7/3 |
| 98 | +Feb last day = (date/month) ==> 4/4, 6/6, 8/8,10/10,12/12, |
| 99 | + 9/5,5/9,11/7,7/11, |
| 100 | + 7/3 |
36 | 101 | jan - 3(nonleap) and 4(leap) |
37 | 102 | |
38 | 103 | eg: if feb 28 - friday |
|
0 commit comments