public class DateUtils {
/**
* 获取当前年份
*
* @return
*/
public static int getYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
/**
* 获取当前月份
*
* @return
*/
public static int getMonth() {
return Calendar.getInstance().get(Calendar.MONTH) + 1;
}
/**
* 获取今天几号
*
* @return
*/
public static int getDate() {
return Calendar.getInstance().get(Calendar.DATE);
}
/**
* 根据传入的年份和月份,获取当前月份的日历分布
*
* @param year
* @param month
* @return
*/
public static int[][] getDayOfMonthFormat(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);//设置时间为每月的第一天
//设置日历格式数组,6行7列
int days[][] = new int[6][7];
//设置该月的第一天是周几
int daysOfFirstWeek = calendar.get(Calendar.DAY_OF_WEEK);
//设置本月有多少天
int daysOfMonth = getDaysOfMonth(year, month);
//设置上个月有多少天
int daysOfLastMonth = getLastDaysOfMonth(year, month);
int dayNum = 1;
int nextDayNum = 1;
//将日期格式填充数组
for (int i = 0; i < days.length; i++) {
for (int j = 0; j < days[i].length; j++) {
if (i == 0 && j < daysOfFirstWeek - 1) {
days[i][j] = daysOfLastMonth - daysOfFirstWeek + 2 + j;
} else if (dayNum <= daysOfMonth) {
days[i][j] = dayNum++;
} else {
days[i][j] = nextDayNum++;
}
}
}
return days;
}
/**
* 根据传入的年份和月份,判断上一个有多少天
*
* @param year
* @param month
* @return
*/
public static int getLastDaysOfMonth(int year, int month) {
int lastDaysOfMonth = 0;
if (month == 1) {
lastDaysOfMonth = getDaysOfMonth(year - 1, 12);
} else {
lastDaysOfMonth = getDaysOfMonth(year, month - 1);
}
return lastDaysOfMonth;
}
/**
* 根据传入的年份和月份,判断当前月有多少天
*
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if (isLeap(year)) {
return 29;
} else {
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
}
return -1;
}
/**
* 判断是否为闰年
*
* @param year
* @return
*/
public static boolean isLeap(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
return false;
}
}
public class DateAdapter extends BaseAdapter {
private int[] days = new int[42];
private Context context;
private int year;
private int month;
private int index;
public DateAdapter(Context context, int[][] days, int year, int month) {
this.context = context;
int dayNum = 0;
//将二维数组转化为一维数组,方便使用
for (int i = 0; i < days.length; i++) {
for (int j = 0; j < days[i].length; j++) {
this.days[dayNum] = days[i][j];
dayNum++;
}
}
index = DateUtils.getDate();
this.year = year;
this.month = month;
}
@Override
public int getCount() {
return days.length;
}
@Override
public Object getItem(int i) {
return days[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.date_item, null);
viewHolder = new ViewHolder();
viewHolder.date_item = (TextView) view.findViewById(R.id.date_item);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (i < 7 && days[i] > 20) {
viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));//将上个月的和下个月的设置为灰色
} else if (i > 20 && days[i] < 15) {
viewHolder.date_item.setTextColor(Color.rgb(204, 204, 204));
}
viewHolder.date_item.setText(days[i] + "");
viewHolder.date_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
index = i;
notifyDataSetChanged();
Toast.makeText(context, year + "年" + month + "月" + days[i], Toast.LENGTH_SHORT).show();
}
});
if (i == index) {
viewHolder.date_item.setBackgroundResource(R.drawable.redbg);
viewHolder.date_item.setTextColor(context.getResources().getColor(R.color.main_bg));
} else {
viewHolder.date_item.setBackgroundResource(R.color.main_bg);
viewHolder.date_item.setTextColor(context.getResources().getColor(R.color.txt0));
}
return view;
}
/**
* 优化Adapter
*/
class ViewHolder {
TextView date_item;
}
}
date_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/date_item"
android:layout_width="20dp"
android:layout_height="20dp"
android:text="28"
android:gravity="center"
android:textSize="12dp" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@BindView(R.id.left_year)
ImageView leftYear;
@BindView(R.id.record_left)
ImageView recordLeft;
@BindView(R.id.record_title)
TextView recordTitle;
@BindView(R.id.record_right)
ImageView recordRight;
@BindView(R.id.right_year)
ImageView rightYear;
@BindView(R.id.record_gridView)
GridView recordGridView;
private int year;
private int month;
private String title;
private int[][] days = new int[6][7];
private DateAdapter dateAdapter;//定义adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//初始化日期数据
initData();
//初始化组件
initView();
}
private void initData() {
year = DateUtils.getYear();
month = DateUtils.getMonth();
}
private void initView() {
/**
* 以下是初始化GridView
*/
days = DateUtils.getDayOfMonthFormat(2016, 8);
dateAdapter = new DateAdapter(this, days, year, month);//传入当前月的年
recordGridView.setAdapter(dateAdapter);
recordGridView.setVerticalSpacing(60);
recordGridView.setEnabled(false);
setTile();
}
/**
* 设置标题
*/
private void setTile() {
title = year + "年" + month + "月";
recordTitle.setText(title);
}
@OnClick({R.id.record_left, R.id.left_year, R.id.right_year, R.id.record_right})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.left_year:
days = prevyear();
dateAdapter = new DateAdapter(this, days, year, month);
recordGridView.setAdapter(dateAdapter);
dateAdapter.notifyDataSetChanged();
setTile();
break;
case R.id.right_year:
days = nextyear();
dateAdapter = new DateAdapter(this, days, year, month);
recordGridView.setAdapter(dateAdapter);
dateAdapter.notifyDataSetChanged();
setTile();
break;
case R.id.record_left:
days = prevMonth();
dateAdapter = new DateAdapter(this, days, year, month);
recordGridView.setAdapter(dateAdapter);
dateAdapter.notifyDataSetChanged();
setTile();
break;
case R.id.record_right:
days = nextMonth();
dateAdapter = new DateAdapter(this, days, year, month);
recordGridView.setAdapter(dateAdapter);
dateAdapter.notifyDataSetChanged();
setTile();
break;
}
}
/**
* 下一个月
*
* @return
*/
private int[][] nextMonth() {
if (month == 12) {
month = 1;
year++;
} else {
month++;
}
days = DateUtils.getDayOfMonthFormat(year, month);
return days;
}
/**
* 上一个月
*
* @return
*/
private int[][] prevMonth() {
if (month == 1) {
month = 12;
year--;
} else {
month--;
}
days = DateUtils.getDayOfMonthFormat(year, month);
return days;
}
/**
* 下一年
*
* @return
*/
private int[][] nextyear() {
year++;
days = DateUtils.getDayOfMonthFormat(year, month);
return days;
}
/**
* 上一年
*
* @return
*/
private int[][] prevyear() {
year--;
days = DateUtils.getDayOfMonthFormat(year, month);
return days;
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/left_year"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:src="@mipmap/icon_left" />
<ImageView
android:id="@+id/record_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:src="@mipmap/icon_left" />
<TextView
android:id="@+id/record_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:maxLines="1"
android:text=""
android:textColor="@color/txt0"
android:textSize="16sp" />
<ImageView
android:id="@+id/record_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@mipmap/icon_right" />
<ImageView
android:id="@+id/right_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@mipmap/icon_right" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="日"
android:textColor="#ffff0000"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="一"
android:textColor="@color/txt0"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="二"
android:textColor="@color/txt0"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="三"
android:textColor="@color/txt0"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="四"
android:textColor="@color/txt0"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="五"
android:textColor="@color/txt0"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="六"
android:textColor="#ffff0000"
android:textSize="16sp" />
</LinearLayout>
<GridView
android:id="@+id/record_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:numColumns="7" />
</LinearLayout>
</LinearLayout>