The other day, I'm reading a book called "Mental Calculation Master" that I found at a second-hand bookstore. There was a ** method to calculate the day of the week from the date **, so when I tried it using my birthday, it matched perfectly.
** Enjoy math! ** **
so,
I tried to put it in the program. I usually use the Calendar class, so it's refreshing to calculate the day of the week by myself.
** Enjoy programming! ** **
package math;
import java.util.HashMap;
import java.util.Map;
/**
 *Calculation of day of the week from date
 * -1900s and 2000s can be calculated
 * -Leap years are not considered
 */
public class CalcDayOfWeek {
	/**Code for the month(key:Month/val:Code) */
	private static final Map<Integer, Integer> SIGN_OF_MONTH = new HashMap<>();
	static {
		SIGN_OF_MONTH.put( 1, 6);
		SIGN_OF_MONTH.put( 2, 2);
		SIGN_OF_MONTH.put( 3, 2);
		SIGN_OF_MONTH.put( 4, 5);
		SIGN_OF_MONTH.put( 5, 0);
		SIGN_OF_MONTH.put( 6, 3);
		SIGN_OF_MONTH.put( 7, 5);
		SIGN_OF_MONTH.put( 8, 1);
		SIGN_OF_MONTH.put( 9, 4);
		SIGN_OF_MONTH.put(10, 6);
		SIGN_OF_MONTH.put(11, 2);
		SIGN_OF_MONTH.put(12, 4);
	}
	/**Code for the day of the week(key:Code/val:Calculation results) */
	private static final Map<Integer, String> SIGN_OF_WEEK  = new HashMap<>();
	static {
		SIGN_OF_WEEK.put(1, "Mon.");
		SIGN_OF_WEEK.put(2, "Tue.");
		SIGN_OF_WEEK.put(3, "Wed.");
		SIGN_OF_WEEK.put(4, "Thu.");
		SIGN_OF_WEEK.put(5, "Fri.");
		SIGN_OF_WEEK.put(6, "Sat.");
		SIGN_OF_WEEK.put(0, "Sun.");
	}
	/**Year(First 2 digits)Specific gravity for*/
	private static final Map<Integer, Integer> WEIGHT_OF_YEAR = new HashMap<>();
	static {
		WEIGHT_OF_YEAR.put(19, 1);
		WEIGHT_OF_YEAR.put(20, 0);
	}
	/**
	 *Main({@link #calcDayOfWeek}Just call)
	 * @param args
	 */
	public static void main(String[] args) {
		calcDayOfWeek("1993/09/17"); //For the time being, my birthday
		calcDayOfWeek("2017/09/17"); //This year's birthday
		calcDayOfWeek("2001/09/11"); //Terrorist attacks in the United States
		calcDayOfWeek("2011/03/11"); //Great East Japan Earthquake
		calcDayOfWeek("1989/11/09"); //Berlin wall collapse
	}
	/**
	 *Day of the week calculation
	 * -Year: 25 last 2 digits%Add the value and the last two digits.(However, the specific gravity is added depending on the age.)
	 * -Month: Obtained from sign mapping to month
	 * -Date: Use as is
	 * @param yyyyMMdd
	 */
	public static void calcDayOfWeek(String yyyyMMdd) {
		if (yyyyMMdd == null)
			return;
		yyyyMMdd = yyyyMMdd.replaceAll("-", "").replaceAll("/", "").replaceAll(" ", "");
		if (!checkFormat(yyyyMMdd))
			return;
		int yy     = Integer.parseInt(yyyyMMdd.substring(2, 4));
		int mm     = Integer.parseInt(yyyyMMdd.substring(4, 6));
		int dd     = Integer.parseInt(yyyyMMdd.substring(6, 8));
		int weight = WEIGHT_OF_YEAR.get(Integer.parseInt(yyyyMMdd.substring(0, 2)));
		int signOfYear  = (yy / 4) + yy + weight;
		int signOfMonth = SIGN_OF_MONTH.get(mm); 
		int signOfDay   = dd;
		System.out.println(String.format("%s is %s",yyyyMMdd, SIGN_OF_WEEK.get((signOfYear + signOfMonth + signOfDay) % 7)));
	}
	private static boolean checkFormat(String yyyyMMdd) {
		try {
			Integer.parseInt(yyyyMMdd);
			return yyyyMMdd.length() == 8;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}
For the time being, my birthday and global news ...
19930917 is Fri.
20170917 is Sun.
20010911 is Tue.
20110311 is Fri.
19891109 is Thu.
・ The calculation itself is not troublesome, but it is difficult to remember the sign mapping (SIGN_OF_MONTH) for the month. ・ The process will be a little more complicated considering the leap year, but it doesn't seem to be that big (* I may update it as soon as I check it). ・ I don't know about the 1800s and 2100s (* I may update it as soon as I check it)
If you can calculate it in your head, you can use it at a joint party and it seems to be popular. (suitable)
Recommended Posts