-----------android培训、java培训、java学习型技术博客、期待与您交流! ------------
一、利用Calendar类实现日期到星期的转换
import java.util.Calendar; import java.util.Scanner; import java.util.regex.Pattern; public class Time { public static void main(String[] args) { //输入日期 System.out.print("请按“yyyy-MM-dd”的格式输入一个日期:"); Scanner scan = new Scanner(System.in); String s = scan.nextLine(); //使用正则表达式拆分日期 String sPattern = "[-]+"; Pattern p1 = Pattern.compile(sPattern); String[] arrWords = p1.split(s); int year = Integer.parseInt(arrWords[0]); int month = Integer.parseInt(arrWords[1]); int day = Integer.parseInt(arrWords[2]); //使用Calendar类 Calendar c1 = Calendar.getInstance(); c1.set(year, month - 1, day); int iWeek = (int)c1.get(Calendar.DAY_OF_WEEK); //输出这一天是星期几 int temp = iWeek -1; if (temp == 1) System.out.println(s + "是星期一"); if (temp == 2) System.out.println(s + "是星期二"); if (temp == 3) System.out.println(s + "是星期三"); if (temp == 4) System.out.println(s + "是星期四"); if (temp == 5) System.out.println(s + "是星期五"); if (temp == 6) System.out.println(s + "是星期六"); if (temp == 0) System.out.println(s + "是星期日"); } }
二、选定一个参考时间点,然后根据输入日期与参考时间点的相差天数推断是星期几
import java.util.Scanner; import java.util.regex.Pattern; public class Test9 { //闰年每个月的天数 static int M[] = {31,29,31,30,31,30,31,31,30,31,30,31}; //非闰年每个月的天数 static int M1[] = {31,28,31,30,31,30,31,31,30,31,30,31}; static int year = 0; static int month = 0; static int day = 0; static int sum_day = 0; static int th_day = 0; //判断是否为闰年 public static boolean check(int year) { if (year % 400 == 0) return true; else if (year % 4 == 0 && year % 100 != 0) return true; else return false; } //算出输入的年份距离2007-01-01的总天数 public static void comCurYear(int year, int flag) { int year_flag = 0; if (check(year)) year_flag = 1; int k; //year >= 2007 if (0 == flag) { for (k = 1; k <= 12; k++) { if (k < month) { if (1 == year_flag) { sum_day += M[k - 1]; th_day += M[k - 1]; } else { sum_day += M1[k - 1]; th_day += M1[k - 1]; } } else if (k == month) { sum_day += day; th_day += day; } else if (k > month) { break; } } } //2007年之前 else { for (k = 12; k >= 1; k--) { if (month < k) { if (year_flag == 1) sum_day += M[k - 1]; else sum_day +=M1[k - 1]; } else if (k == month) { if (year_flag == 1) sum_day += M[k - 1] - day; else sum_day +=M1[k - 1] - day; } else { break; } } //2007年之前需要做一个处理 int temp = sum_day % 7; sum_day = sum_day - temp + 7 - temp; //计算是一年中的第几天 for (k = 1; k < 12; k++) { if (k < month) { if (1 == year_flag) th_day += M[k - 1]; else th_day += M1[k - 1]; } else if (k == month) { if (1 == year_flag) th_day += day; else th_day += day; } else if (k > month) { break; } } } return; } public static void main(String[] args) { //输入日期 System.out.print("请按“yyyy-MM-dd”的格式输入一个日期:"); Scanner scan = new Scanner(System.in); String s = scan.nextLine(); //使用正则表达式拆分日期 String sPattern = "[-]+"; Pattern p1 = Pattern.compile(sPattern); String[] arrWords = p1.split(s); year = Integer.parseInt(arrWords[0]); month = Integer.parseInt(arrWords[1]); day = Integer.parseInt(arrWords[2]); //2007-01-01是星期一 //2007年之后 if (year > 2007) { int i; for (i = 2007; i < year; i++) { if (check(i)) sum_day += 366; else sum_day += 365; } comCurYear(year, 0); } else if (2007 == year) { comCurYear(year, 0); } else { int i; for (i = 2006; i > year; i--) { if (check(i)) sum_day += 366; else sum_day += 365; } comCurYear(year, 1); } //输出这一天是星期几 int temp = sum_day % 7; if (temp == 1) System.out.println(s + "是星期一" + ",是一年中的第" + th_day + "天"); if (temp == 2) System.out.println(s + "是星期二" + ",是一年中的第" + th_day + "天"); if (temp == 3) System.out.println(s + "是星期三" + ",是一年中的第" + th_day + "天"); if (temp == 4) System.out.println(s + "是星期四" + ",是一年中的第" + th_day + "天"); if (temp == 5) System.out.println(s + "是星期五" + ",是一年中的第" + th_day + "天"); if (temp == 6) System.out.println(s + "是星期六" + ",是一年中的第" + th_day + "天"); if (temp == 0) System.out.println(s + "是星期日" + ",是一年中的第" + th_day + "天"); } }