大三了,基本上没有课了,实习那事还没有定下来,所以在闲来无事的时候,写了个日历。这个日历基本上与OPPO手机上的日历差不多,不过没有没有备忘录和统计周数的功能。

       开发准备:

       IDE:eclipse

      语言:java

      适用于:Android智能手机

      AndroidSDK:4(鉴于兼容性的考虑,使用了版本比较低的SDK)

      本次开发主要需要解决两个问题:

1、某年某月某日是周几(主要是获取每月第一天是星期几)

2、获取每月多少天

       对于第二个问题比较好解决,在此就不再累赘了,现在我们来看看第一个问题的解决,对于第一个问题有个公式:

某年某月某日是周几=(年+年/4+年/400-年/100-年基数+月基数+日)%7

  年基数:平年1,闰年2,

  月基数:

1、平年:一月0, 二月3, 三月3, 四月6, 五月1, 六月4,七月0(-1), 八月3(2 ), 九月5, 十月0, 十一月3, 十二月5.

    2、闰年:一月0, 二月3, 三月4, 四月0, 五月2, 六月5,七月0, 八月3, 九月6, 十月1, 十一月4, 十二月6.

        查的资料说是,平年的时候七月和八月的月基数分别是0和3,实践证明平年的时候七月和八月的月基数分别是-1和2

下面和大家分享几个算法,希望初学者看到的话可以帮助你们,如果是大牛看到的话可以给些算法指点

/*
      * 判断给定的一个年份到底是闰年还是平年
      * */
  private boolean isLeap (int year){
   int yearInt = year;
   if (yearInt%400==0 || (yearInt%4==0 && yearInt%100!=0)) {
    return true;
   } else {
    return false;
   }
  }
   
  /*
   * 计算给定年份的基数:闰年的年基数是2,平年的年基数是1
   * */
  private int yearBase(int year) {
   int yearInt = year;
   boolean isLeap = isLeap(yearInt);
   if (isLeap) {
    return 2;
   } else {
    return 1;
   }
  }
  /******************************
   *计算给定年份和月份的月基数
   * ****************************/
  
  private int monthBase(int year, int month) {
   int monthBase = 0;
   if (isLeap(year)) {
    switch (month) {
    case 1:
    case 4:
    case 7:
     monthBase = 0;
     break;
    case 2:
    case 8:
     monthBase = 3;
     break;
    case 3:
    case 11:
     monthBase = 4;
     break;
    case 5:
     monthBase = 2;
     break;
    case 6:
     monthBase = 5;
     break;
    case 9:
    case 12:
     monthBase = 6;
     break;
    case 10:
     monthBase = 1;
     break;
    default:
     break;
    }
   } else {
    switch (month) {
    case 1:
    case 10:
     monthBase = 0;
     break;
    case 2:
    case 3:
    case 11:
     monthBase = 3;
     break;
    case 4:
     monthBase = 6;
     break;
    case 5:
     monthBase = 1;
     break;
    case 6:
     monthBase = 4;
     break;
    case 9:
    case 12:
     monthBase = 5;
     break;
    case 8:
     monthBase = 2;
     break;
    case 7:
     monthBase = -1;
     break;
    default:
     break;
    }
   }
   return monthBase;
  }
  
  /*
   * 计算某年某月某日是周几的算法0-6
   * */  
  private int week(int year, int month, int day) {
   int week = 7;
   int yearInt = year;
   int monthInt = month;
   int dayInt = day;
   week = (yearInt + yearInt/4 + yearInt/400 - yearInt/100 - yearBase(yearInt) + monthBase(yearInt, monthInt) + dayInt)%7;
   return week;
  }
  
  /*
   * 计算每个月有多少天
   * */
  private int countDayOfMonth(int year, int month) {
   int dayOfMonth = 0;
   switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
     dayOfMonth = 31;
     break;
    case 4:
    case 6:
    case 9:
    case 11:
     dayOfMonth = 30;
     break;
    case 2:
     if (isLeap(year)) {
      dayOfMonth = 29;
     } else {
      dayOfMonth = 28;
     }
     break;
    default:
     break;
    }
   
   return dayOfMonth;
  }
  
 /*
   * 重写onKeyDown()函数
   * */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
   String direction = "";
   if (keyCode==KeyEvent.KEYCODE_DPAD_DOWN && event.getRepeatCount() == 0) {
    direction = "down";
    focus = focus + 7;
    upDownLeftRight(direction);
    return true;
   }
   if (keyCode==KeyEvent.KEYCODE_DPAD_UP && event.getRepeatCount() == 0) {
    direction = "up";
    focus = focus - 7;
    upDownLeftRight(direction); 
    return true;
   }
   if (keyCode==KeyEvent.KEYCODE_DPAD_LEFT && event.getRepeatCount() == 0) {
    direction = "left";
    focus = focus - 1;
    upDownLeftRight(direction); 
    return true;
   }
   if (keyCode==KeyEvent.KEYCODE_DPAD_RIGHT && event.getRepeatCount() == 0) {
    direction = "right";
    focus = focus + 1;
    upDownLeftRight(direction);
    return true;
   }
   return super.onKeyDown(keyCode, event);
  }
  
 /*
   * 对于上下键的处理,月和年变化时的处理
   * */
  private void MonthYearChange(String s) {
   String dire = s;
   if (dire.equals("down")) {
    if (whichMonth<12) {
     whichMonth = whichMonth + 1; 
    } else {
     whichMonth = 1;
     whichYear = whichYear + 1;
    }
   } else if (dire.equals("up")) {
    if (whichMonth>1) {
     whichMonth = whichMonth - 1;
    } else {
     whichMonth = 12;
     whichYear = whichYear - 1;
    }
   } else if (dire.equals("left")){
    if (whichMonth>1) {
     whichMonth = whichMonth - 1;
    } else {
     whichMonth = 12;
     whichYear = whichYear - 1;
    }
   } else if (dire.equals("right")) {
    if (whichMonth<12) {
     whichMonth = whichMonth + 1;
    } else {
     whichMonth = 1;
     whichYear = whichYear + 1;
    }
   }
   this.onResume(); 
  }


 

        以上算法都十分的简单并且每个算法针对其功能都有注释,有点语言基础的人看懂都不会有问题。

总结:

        该次项目在代码量虽然没有上次写的计算器上量大,但是我觉着这次做的项目比上次的项目有思考力度,还有该次项目中我尽量的将代码模块化,以体现“高内聚,低耦合”的思想,还有就是对变量的控制访问有了更多的考虑。

 

        我会尽快将源码上传,希望大家积极体验,学习!