紀元前後情報と年月日情報からの曜日特定

メリットは特にないと思いますがjava.util.Calendar.*を使わずに、CE/BCE(紀元前後)情報と年月日情報から曜日を特定し出力します。

1582CEの10月5日から14日までは存在しません。それ以前はJulian暦、それ以後はGregorian暦を採用します。実際にJulian暦からGregorian暦に移行したのは1582CEの2月24日ですが、2月24日から10月4日までの間にJulian暦とGregorian暦に違いがないためそのようにしました。また、Julian暦の制定された45BCE以前は存在しません。

Wikipediaにも書いてあるように、4CEは閏年ではないと考え1CEの元旦は日曜日だと主張する人もいるようですが、(直すの簡単だし)今回は不採用とします。
http://en.wikipedia.org/wiki/1
なので、Julian暦というよりProleptic Julian暦と言った方が正確かもしれません。

それと、BCEの閏年判定についてですが、CEの年数に変換して(Proleptic) Julian暦による判定を行います。知恵袋で質問しても納得のいく回答が得られませんでした。暦制定はその時点以前の整合性を考えていないような気がしてきました。
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail.php?qid=1178294221

main関数のistheCommonEraとyear、month、dayを変えて実行してください。

構成

  • Weekdayクラス(main関数あり)
  • CalendarInterface
  • RomanCalendarクラス
  • GregorianCalendarクラス
  • JulianCalendarクラス
  • October1582クラス
  • CE45クラス
  • Weekdayクラス(main関数あり)
public class Weekday {
	static String weekday(int correction) {
		switch (correction) {
			case 0: return "Sunday";
			case 1: return "Monday";
			case 2: return "Tuesday";
			case 3: return "Wednesday";
			case 4: return "Thursday";
			case 5: return "Friday";
			case 6: return "Saturday";
			default : return "error";
		}
	}
	
	public static void main(String[] args) {	
		boolean istheCommonEra = true;
		int year = 1, month = 1, day = 1;
		String weekday = RomanCalendar.isCalendar(istheCommonEra, year, month, day)
			? weekday(RomanCalendar.correction(istheCommonEra, year, month, day))
			: "This day does NOT exist.";
		System.out.println(weekday);
	}

}
  • CalendarInterface
public interface CalendarInterface {
	boolean isCalendar(boolean istheCommonEra, int year, int month, int day);
	boolean istheCommonEra(int year, int month, int day);
	boolean isBeforetheCommonEra(int year, int month, int day);
	boolean isYear(int year);
	boolean isLeapYear(boolean istheCommonEra, int year);
	boolean isLeapYearOftheCommonEra(int year);
	boolean isLeapYearOfBeforetheCommonEra(int year);
	boolean isMonth(int month);
	boolean isDay(boolean isLeapYear, int month, int day);
	int getParameterForYearCorrection(boolean istheCommonEra, int month);
	int correctedByYear(int year, int parameter);
	int correctedByMonth(int month);
	int correctedByDay(int day);
	int correctedBytheCommonEra(int year, int month, int day);
	int correctedByBeforetheCommonEra(int year, int month, int day);
	int correctedByCalendar = 0;
	int correction(boolean istheCommonEra, int year, int month, int day);
}
  • RomanCalendarクラス
public class RomanCalendar {
	static int correction(boolean istheCommonEra, int year, int month, int day) {
		return October1582.isAfterOctober1582(istheCommonEra, year, month, day) 
			? (new GregorianCalendar()).correction(istheCommonEra, year, month, day)
			: (new JulianCalendar()).correction(istheCommonEra, year, month, day);
	}
	
	static boolean isCalendar(boolean istheCommonEra, int year, int month, int day) {
		return CE45.isAfter46CE(istheCommonEra, year)
			? October1582.isOctober1582(istheCommonEra, year, month, day)
				? false
				: October1582.isAfterOctober1582(istheCommonEra, year, month, day)
					?(new GregorianCalendar()).isCalendar(istheCommonEra, year, month, day)
					:(new JulianCalendar()).isCalendar(istheCommonEra, year, month, day)
			: false;
	}
}
  • GregorianCalendarクラス
public class GregorianCalendar extends October1582 implements CalendarInterface {
	public int correctedByCalendar = 2;
	
	public int getParameterForYearCorrection(boolean istheCommonEra, int month) {
		int parameter = 0;
		if (!istheCommonEra) parameter++;
		if (month == 1 || month == 2) parameter++;
		return parameter;
	}
	
	public int correctedByYear(int year, int parameter) {
		//year -= year / 10000 * 10000;
		year -= parameter;
		return year + year / 4 - year / 100 + year / 400 + parameter;
	}
	
	public int correctedByMonth(int month) {
		if (month == 5) {
			return 1;
		} else if (month == 8) {
			return 2;
		} else if (month == 2 || month == 3 || month == 11) {
			return 3;
		} else if (month == 6) {
			return 4;
		} else if (month == 9 || month == 12) {
			return 5;
		} else if (month == 4 || month == 7) {
			return 6;
		} else {
			return 0;
		}
	}
	
	public int correctedByDay(int day) {
		return day;
	}
	
	public int correctedBytheCommonEra(int year, int month, int day) {
		int c = correctedByMonth(month)
				+ correctedByDay(day)
				+ correctedByYear(year, getParameterForYearCorrection(true, month))
				+ correctedByCalendar
				+ correctedByOctober1582(true, year, month, day);
		c %= 7;
		return c < 0 ? c + 7 : c;
	}
	
	public int correctedByBeforetheCommonEra(int year, int month, int day) {
		int c = correctedByMonth(month) 
				+ correctedByDay(day)
				+ correctedByYear(year, getParameterForYearCorrection(false, month))
				+ correctedByCalendar;
		c %= 7;
		return c < 0 ? c + 7 : c;
	}
	
	public int correction(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra ? correctedBytheCommonEra(year, month, day) : correctedByBeforetheCommonEra(year, month, day);
	}
	
	//check
	public boolean isYear(int year) {
		return year > 0 ? true : false;
	}
	
	public boolean isLeapYear(boolean istheCommonEra, int year) {
		return istheCommonEra ? isLeapYearOftheCommonEra(year) : isLeapYearOfBeforetheCommonEra(year);
	}
	
	public boolean isLeapYearOftheCommonEra(int year) {
		return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) ? true : false;
	}
	
	public boolean isLeapYearOfBeforetheCommonEra(int year) {
		return isLeapYearOftheCommonEra(year - 1);
	}
	
	public boolean isMonth(int month) {
		return month > 0 && month < 13 ? true : false;
	}
	
	public boolean isDay(boolean isLeapYear, int month, int day) {
		return day > 0
			? month == 4 || month == 6 || month == 9 || month == 11 
				? day < 31
					? true : false 
				: month == 2
					? isLeapYear 
						? day < 30 ? true : false
					    : day < 29 ? true : false
					: day < 32 ? true : false
			: false;
	}
	
	public boolean istheCommonEra(int year, int month, int day) {
		return isYear(year) && isMonth(month) && isDay(isLeapYear(true, year), month, day) && !isOctober1582(true, year, month, day);
	}
	
	public boolean isBeforetheCommonEra(int year, int month, int day) {
		return isYear(year) && isMonth(month) && isDay(isLeapYear(false, year), month, day);
	}
	
	public boolean isCalendar(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra ? istheCommonEra(year, month, day) : isBeforetheCommonEra(year, month, day);
	}
}
  • JulianCalendarクラス
public class JulianCalendar extends October1582 implements CalendarInterface {	
	public int correctedByCalendar = 4;
	
	public int getParameterForYearCorrection(boolean istheCommonEra, int month) {
		int parameter = 0;
		if (!istheCommonEra) parameter++;
		if (month == 1 || month == 2) parameter++;
		return parameter;
	}
	
	public int correctedByYear(int year, int parameter) {
		//year -= year / 10000 * 10000;
		year -= parameter;
		return year + year / 4 + parameter;
	}
	
	public int correctedByMonth(int month) {
		if (month == 5) {
			return 1;
		} else if (month == 8) {
			return 2;
		} else if (month == 2 || month == 3 || month == 11) {
			return 3;
		} else if (month == 6) {
			return 4;
		} else if (month == 9 || month == 12) {
			return 5;
		} else if (month == 4 || month == 7) {
			return 6;
		} else {
			return 0;
		}
	}
	
	public int correctedByDay(int day) {
		return day;
	}
	
	public int correctedBytheCommonEra(int year, int month, int day) {
		int c = correctedByMonth(month)
				+ correctedByDay(day)
				+ correctedByYear(year, getParameterForYearCorrection(true, month))
				+ correctedByCalendar
				+ correctedByOctober1582(true, year, month, day);
		c %= 7;
		return c < 0 ? c + 7 : c;
	}
	
	public int correctedByBeforetheCommonEra(int year, int month, int day) {
		int c = correctedByMonth(month) 
				+ correctedByDay(day)
				+ correctedByYear(year, getParameterForYearCorrection(false, month))
				+ correctedByCalendar;
		c %= 7;
		return c < 0 ? c + 7 : c;
	}
	
	public int correction(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra ? correctedBytheCommonEra(year, month, day) : correctedByBeforetheCommonEra(year, month, day);
	}
	
	//check
	public boolean isYear(int year) {
		return year > 0 ? true : false;
	}
	
	public boolean isLeapYear(boolean istheCommonEra, int year) {
		return istheCommonEra ? isLeapYearOftheCommonEra(year) : isLeapYearOfBeforetheCommonEra(year);
	}
	
	public boolean isLeapYearOftheCommonEra(int year) {
		return year % 4 == 0 ? true : false;
	}
	
	public boolean isLeapYearOfBeforetheCommonEra(int year) {
		return isLeapYearOftheCommonEra(year - 1);
	}
	
	public boolean isMonth(int month) {
		return month > 0 && month < 13 ? true : false;
	}
	
	public boolean isDay(boolean isLeapYear, int month, int day) {
		return day > 0
			? month == 4 || month == 6 || month == 9 || month == 11 
				? day < 31
					? true : false 
				: month == 2
					? isLeapYear 
						? day < 30 ? true : false
					    : day < 29 ? true : false
					: day < 32 ? true : false
			: false;
	}
	
	public boolean istheCommonEra(int year, int month, int day) {
		return isYear(year) && isMonth(month) && isDay(isLeapYear(true, year), month, day) && !isOctober1582(true, year, month, day);
	}
	
	public boolean isBeforetheCommonEra(int year, int month, int day) {
		return isYear(year) && isMonth(month) && isDay(isLeapYear(false, year), month, day);
	}
	
	public boolean isCalendar(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra ? istheCommonEra(year, month, day) : isBeforetheCommonEra(year, month, day);
	}
}
  • October1582クラス
public class October1582 {
	static int correctedByOctober1582(boolean istheCommonEra, int year, int month, int day) {
		return isAfterOctober1582(istheCommonEra, year, month, day) ? -10 : 0; 
	}
	
	static boolean isOctober1582(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra && year == 1582 && month == 10 && day > 4 && day < 15 ? true : false;
	}
	
	static boolean isAfterOctober1582(boolean istheCommonEra, int year, int month, int day) {
		return istheCommonEra && (year > 1582 || year == 1582 && (month > 10 || month == 10 && day > 14));
	}
}
  • CE45クラス
public class CE45 {
	static boolean isAfter46CE(boolean istheCommonEra, int year) {
		return istheCommonEra ? true : year < 46 ? true : false;
	}
}