万年历实现源代码

/**
* 日期工具类
*
*/
public class DateUtils {

        /**
         * 某月的天数
         */
        private static int daysOfMonth = 0;

        /**
         * 具体某一天是星期几
         */
        private static int dayOfWeek = 0;

        /**
         * 判断是否为闰年
         *
         * @param year
         *            指定的年份
         * @return
         */
        public static boolean isLeapYear(int year) {
                if (year % 100 == 0 && year % 400 == 0) {
                        return true;
                } else if (year % 100 != 0 && year % 4 == 0) {
                        return true;
                }
                return false;
        }

        /**
         * 得到某月有多少天数
         *
         * @param isLeapyear
         *            目标年份
         * @param month
         *            目标月份
         * @return
         */
        public static int getDaysOfMonth(boolean isLeapyear, int month) {
                switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                        daysOfMonth = 31;
                        break;
                case 4:
                case 6:
                case 9:
                case 11:
                        daysOfMonth = 30;
                        break;
                case 2:
                        if (isLeapyear) {
                                daysOfMonth = 29;
                        } else {
                                daysOfMonth = 28;
                        }

                }
                return daysOfMonth;
        }

        /**
         * 指定某年中的某月的第一天是星期几
         *
         * @param isLeapyear
         *            目标年份
         * @param month
         *            目标月份
         * @return
         */
        public static int getWeekdayOfMonth(int year, int month) {
                Calendar cal = Calendar.getInstance();
                cal.set(year, month - 1, 1);
                dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
                return dayOfWeek;
        }

        /**
         * 获取当前年份与月份
         *
         * @return 返回日期数组,整形array[0],为年份,array[1]为月份, array[2]为日期
         */
        public static int[] getCurrentYearAndMonth() {
                int[] result = new int[3];
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
                String str = "";
                Date date = new Date();

                str = sdf.format(date); // 当期日期
                result[0] = Integer.parseInt(str.split("-")[0]);
                result[1] = Integer.parseInt(str.split("-")[1]);
                result[2] = Integer.parseInt(str.split("-")[2]);

                return result;
        }
}


//日历适配器:
/**
* 日历适配器
*/
public class CalendarAdapter extends BaseAdapter{
        private Context context;
        private TextView dateText;
       
        /**
         * 时间
         */
        private String[] dateTime;
       
        /**
         * 判断是不是当月的日期
         */
        private int[] dayFlag;
       
        /**
         *判断是否是已签到日
         */
        private int[] signedFlag;
       
        public CalendarAdapter(Context context, String[] dateTime, int[] dayFlag, int[] signedFlag){
                this.context = context;
                this.dateTime = dateTime;
                this.dayFlag = dayFlag;
                this.signedFlag = signedFlag;
        }

        @Override
        public int getCount() {
                // TODO Auto-generated method stub
                return dateTime.length;
        }

        @Override
        public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                dateText = new TextView(context);
               
                dateText.setGravity(Gravity.CENTER);
                dateText.setTextSize(18);
                dateText.setWidth(46);
                dateText.setHeight(56);
                dateText.setPadding(5, 5, 5, 5);
                dateText.setText(dateTime[position]);
               
                //如果不是当月的日期
                if(position > 6 && dayFlag[position] == 0){
                        dateText.setTextColor(Color.parseColor("#303030"));
                }else if(position > 6 && dayFlag[position] == 1){
                        dateText.setTextColor(Color.parseColor("#7F7F7F"));
                }else if(position < 7){
                        dateText.setTextColor(Color.parseColor("#7F7F7F"));
                }
               
                //判断是否为周末,改变字体颜色
                if(position > 6 && position % 7 == 0){
                        dateText.setTextColor(Color.parseColor("#FF5F20"));
                       
                }else if(position > 6 && (position+1) % 7 ==0){
                        dateText.setTextColor(Color.parseColor("#FF5F20"));
                }
               
                //判断是否为历史签到日,是则改变背景
                if(dayFlag[position] == 1){
                        for(int index=0; index= 7; index--, lastDaysOFMonth--) {
                        monthData[index] = lastDaysOFMonth + "";
                        dayFlag[index] = 0;
                }

                // 当月的天数
                for (num = 1, index = 7 + firstDayOfMonth; index < 7 + firstDayOfMonth
                                + daysOFMonth; index++, num++) {
                        monthData[index] = num + "";
                        dayFlag[index] = 1;
                }

                // 显示下个月的日期
                for (num = 1, index = 7 + firstDayOfMonth + daysOFMonth; index < 49; index++, num++) {
                        monthData[index] = num + "";
                        dayFlag[index] = 0;
                }

                initSignedFlag(year, month);

                calAdapter = new CalendarAdapter(this, monthData, dayFlag, daySignedFlag);
                calGridView.setAdapter(calAdapter);
                calGridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); // 去除gridView边框
        }

转自:http://www.eoeandroid.com/thread-314996-1-1.html

你可能感兴趣的:(Android)