根据某年某月某日获得这一天是星期几之Java篇

package com.xcl.common.util;

public class DateUtil {

	/**
	 * 根据 年月日,获得这一天是星期几
	 * 
	 * 如: 根据2010-07-20 ,得到这天是星期二
	 */
	public static String getWeekByYMD(String dateStr, String regix)
			throws Exception {
		String[] arr = dateStr.split(regix);

		int uYear = Integer.parseInt(arr[0], 10);
		int uMonth = Integer.parseInt(arr[1], 10);
		int uDay = Integer.parseInt(arr[2], 10);
		int a = (14 - uMonth) / 12;
		int y = uYear - a;
		int m = uMonth + 12 * a - 2;
		int d = (uDay + y + y / 4 - y / 100 + y / 400 + 31 * m / 12) % 7;
		String[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		return week[d];
	}
}



public static void main(String[] args) {
    String dateStr = "2010-07-20";
    System.out.println(getWeekByYMD(dateStr, "-"));
}

你可能感兴趣的:(java)