• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java如何使用矩阵法计算某一天是星期几

JAVA相关 水墨上仙 1388次浏览

Java通过矩阵法计算某一天是星期几,此代码适用于1900年后的日期计算

代码转自:http://blog.csdn.net/fulei_master/

package com.haliluya.java.oj;

import java.util.Scanner;

public class judgyDay {
	
	private static final int  startYear = 1900;
	private static final int[][] dayMap = { 	{0,1,2,3,4,5,6},
												{1,2,3,4,5,6,0},
												{2,3,4,5,6,0,1},												
												{3,4,5,6,0,1,2},
												{4,5,6,0,1,2,3},
												{5,6,0,1,2,3,4},
												{6,0,1,2,3,4,5}
											};
	
	public static void main(String[] args) {
		
		Scanner s = new Scanner(System.in);
		System.out.println("请输入年");
		int year = s.nextInt();
		System.out.println("请输入月");
		int month = s.nextInt();
		System.out.println("请输入号");
		int hao = s.nextInt();
				
		
		System.out.println("输入的年 = "+year);
		System.out.println("输入的月 = "+month);
		System.out.println("输入的日 = "+hao);
		
		int dayOfWant = 0;
		
		dayOfWant = judgyDayOfAnyYearAnyMonth(year,month,hao);
		
		System.out.println("对应的星期为 (从0算) = "+dayOfWant);
	}

	/**
	 * 根据输入的年 月以及号 得到改天的星期数
	 * @param year
	 * @param month
	 * @param hao
	 * @return
	 */
	private static int judgyDayOfAnyYearAnyMonth(int year, int month, int hao) {
		
		//参数检查		
		if(year< 1900 ||year >9999 ){
			return -1;
		}
		if(month<0 ||month >12){
			return -1;
		}
		if(hao<0 || hao>31 ){
			return -1;
		}
		
		//业务开始
		//1 算出该月1号距离1900年1月1号多少天
		int daysOfLastMonthTotal = gatDaysOfLastMonthTotal(year,month);
		//2 的出该月1号是星期几
		int firstDay = daysOfLastMonthTotal %7;
		System.out.println("给定的月初星期几(从0算)  = "+(firstDay));
		
		//3 算出该号是星期几
		int dayJudgyByFirstDay = getDayJudgyByFirstDay(firstDay,hao);

		
		return dayJudgyByFirstDay;
	}

	private static int getDayJudgyByFirstDay(int firstDay, int hao) {
		hao = hao%7;
		if(hao ==0){
			hao =7;
		}
		hao -=1;//调整到0下标
		System.out.println("取map的第 "+firstDay + "行,第"+ hao +"列");
		return dayMap[firstDay][hao];
	}

	/**
	 * 算出给定年月的第一天的距离1900的天数
	 * @param year
	 * @param month
	 * @return
	 */
	private static int gatDaysOfLastMonthTotal(int year, int month) {
		// 先算年的求和
		int totalday = 0;
		for(int thisYear = startYear+1 ;thisYear<=year ;thisYear++){
			totalday += getTotalDaysByYear(thisYear);
		}
		
		//再不上月的求和
		for(int i = 1;i <month;i++){
			
			switch(i){
			case 1: 
			case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10: 
			case 12: totalday+= 31;break;
			case 2: 
				if(isRunYear(year)){
					totalday+= 29;break;
				}else
				{
					totalday+= 28;break;
				}				
			case 4: 
			case 6: 
			case 9: 
			case 11:totalday+= 30;break; 
			}
		}
		
		System.out.println("给定年月的第一天距离1900的天数  = "+totalday);
		
		return totalday;
	}

	/**
	 * 判断该年有多少天
	 * @param thisYear
	 * @return
	 */
	private static int getTotalDaysByYear(int thisYear) {
		if((thisYear%4 ==0 && thisYear %100!= 0)||thisYear%400 ==0){
			return 366;//二月多一天 29天
		}
		return 365;
	}
	
	private static boolean isRunYear(int thisYear){
		return ((thisYear%4 ==0 && thisYear %100!= 0)||thisYear%400 ==0);
	}

}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java如何使用矩阵法计算某一天是星期几
喜欢 (0)
加载中……