/**
* 日期工具类
*
*/
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<signedFlag.length; index++){
if(dateTime[position].equals("" + signedFlag[index])){
dateText.setBackgroundColor(Color.parseColor("#FEDED2"));
//如果是最新签到的日期,改变字体,背景
if(index == 0){
dateText.setBackgroundColor(Color.parseColor("#FF5F20"));
dateText.setTextColor(Color.parseColor("#FFFFFE"));
}
}
}


}

return dateText;
}

}


//最后是使用时的方法
/**
* 当月的天数
*/
private String[] monthData;

/**
* 历史签到数
*/
private String[] historySignRecord;

/**
* 判断是否为当月的
*/
private int[] dayFlag;

/**
* 是否为已签到日
*/
private int[] daySignedFlag;

/**
* 当前年月日日期
*/
private int[] currentDate;

/**
* 一个月中的第一天是星期几
*/
private int firstDayOfMonth;

/**
* 一个月的总天数
*/
private int daysOFMonth;
/**
* 上个月的总天数
*/
private int lastDaysOFMonth;
private int tempYear;
private int tempMonth;
private int index;


/**
* 初始化日历相关数据
*
* @param year
* 指定年份
* @param month
* 指定月份
*/
private void initCalData(int year, int month) {
// 当月日历显示在49格中
int num;
monthData = new String[49];
dayFlag = new int[49];

firstDayOfMonth = DateUtils.getWeekdayOfMonth(year, month);

currentDateText.setText(year + "年" + month + "月");

// 判断是否为闰年
if (DateUtils.isLeapYear(year)) {
daysOFMonth = DateUtils.getDaysOfMonth(true, month);
lastDaysOFMonth = DateUtils.getDaysOfMonth(true, month - 1);
} else {
daysOFMonth = DateUtils.getDaysOfMonth(false, month);
lastDaysOFMonth = DateUtils.getDaysOfMonth(false, month - 1);
}

// 前7个格子为周一到周日文字说明
for (index = 0; index < 7; index++) {
monthData[index] = ShopBaseMessage.WEEK_DATA[index];
dayFlag[index] = 0;
}

// 显示上个月的日期
if (firstDayOfMonth == 0) {
firstDayOfMonth = 7;
}

// 如果是周日为第一天,则留一行上月的天数
for (index = 7 + firstDayOfMonth - 1; 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边框
}

 
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()