# 日期类，根据日期计算是本年度的第几天，第几周，星期几（改进版）

xiaoxiao2021-02-28  32

2018,2.28号发过一篇关于日期类的文章但是在后来测试的过程中发现了BUG,是关于计算周几这个功能的BUG，在测试数据时我是以2018年测试的数据，而2018的一月一号刚好是星期一，后来我换到别的年发现一月一号是星期几与计算周几有很大的关系，所以我们要确立本年度一月一号是星期几。经过完善（代码如下） public class MyDate { private int year; private int month; private int day; String[] wekesday = new String[] { "星期四", "星期五", "星期六", "星期天", "星期一", "星期二", "星期三" }; int[] week = new int[] { 4, 5, 6, 7, 1, 2, 3 }; public MyDate() { super(); } public MyDate(int year, int month, int day) { if (!feifapanduan(year, month, day)) throw new MyDateException(); this.year = year; this.month = month; this.day = day; } /** * 本年度第几天 * * @return */ public int day() { int years = this.year; int months = this.month; int days = this.day; /* * 计算日期在本年度的天数 */ int sum = 0; for (int i = 1; i < months; i++) { sum += ym(years, i); } // 返回本年度多少天 return sum + days; } /** * 第几周 */ public void weeks() { int sum = day(); int weekday = this.yiyueyihao(); int weeks = sum / 7; if (sum <= 7 - weekday) { int week = weeks + 1; System.out.println("第" + week + "周"); } else if (sum == 7) { System.out.println("第" + weeks + "周"); } else if ((sum - 7 + weekday) % 7 == 0) { int week = (sum - 7 + weekday) / 7 + 1; System.out.println("第" + week + "周"); } else if (weekday == 7 && (sum - 7 + weekday) % 7 != 0) { int weekss = (sum - 7 + weekday) / 7 + 1; System.out.println("第" + weekss + "周"); } else { int weekss = (sum - 7 + weekday) / 7 + 2; System.out.println("第" + weekss + "周"); } } /** * 星期几 */ public int weekxingqi() { String[] wekesday = this.wekesday; int year = this.year; int sum = day(); /** * * */ int yearsum = 0; for (int a = 1970; a < year; a++) { if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) { yearsum += 366; } else { yearsum += 365; } } // 定义距70年的天数 int ymsum = yearsum + sum; // 天数对7取余 int weekday = (ymsum) % 7; int weeks = ((weekday + 7) - 1) % 7; System.out.println("相距70年" + ymsum + " " + "余数" + weekday); /** * 循环星期数据根据余数找出星期几 */ for (int i = 0; i < wekesday.length; i++) { if (i == weeks) { System.out.println("今天是" + wekesday[i]); } } return yearsum; } /** * 判断是否闰年，选择月份 * * @param years * @param month * @return */ public static int ym(int years, int month) { int days = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if (years % 4 == 0 && years % 100 != 0 || years % 400 == 0) { days = 29; } else { days = 28; } break; default: break; } return days; } /** * 本年度一月一号距离70年一月一号的天数取余得到本年度一月一号是星期几 * * @return */ public int yiyueyihao() { int[] zhou = this.week; // 本年度一月一号距70年的天数取余 int tianshu = this.weekxingqi() % 7; int i; int a = 0; for (i = 0; i < zhou.length; i++) { if (i == tianshu) { a = zhou[i]; break; } } System.out.println("本年度一月一号\t星期" + a); return a; } /** * 判断输入日期是否合法 * * @param year * @param month * @param ri * @return */ public static boolean feifapanduan(int year, int month, int ri) { if (year != 0 && month > 0 && month < 13 && ri > 0 && ri < ym(year, month)) { return true; } return false; } @SuppressWarnings("static-access") public static void main(String[] args) { // TODO Auto-generated method stub MyDate date = new MyDate(2017, 2, 5); System.out.println("本年度第" + date.day() + "天"); date.weeks(); date.weekxingqi(); } }