-
- package org.leelin.attendance.util;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.List;
- public class CalendarTest {
- public static void main(String[] args) throws ParseException {
- List<DayInfo> list = new CalendarTest().genDayInfoList(2008, 12);
- System.out.println(list);
- }
- public List<DayInfo> genDayInfoList(int year, int month) throws ParseException {
- Calendar calendar = GregorianCalendar.getInstance();
- int curDay = calendar.get(Calendar.DATE);
- calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month + "-01"));
- List<DayInfo> list = new ArrayList<DayInfo>();
- int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
- int weekOfFirstDay = calendar.get(Calendar.DAY_OF_WEEK);
- for (int i = 1; i <= days; i++) {
- DayInfo dayInfo = new DayInfo();
- if (curDay == i) {
- dayInfo.setSelected(true);
- }
- dayInfo.setId(i);
- dayInfo.setName(this.getWeekName((i + weekOfFirstDay - 2) % 7));
- list.add(dayInfo);
- }
- return list;
- }
- private String getWeekName(int week) {
- String[] weeks = new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
- return weeks[week];
- }
- }
- public class DayInfo {
- private int id;
- private String name;
- private boolean selected=false;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public boolean isSelected() {
- return selected;
- }
- public void setSelected(boolean selected) {
- this.selected = selected;
- }
- @Override
- public String toString() { // AutoGenerate
- StringBuilder sb = new StringBuilder();
- sb.append("<");
- sb.append(" id = ").append(this.id);
- sb.append(" name = ").append(this.name);
- sb.append(" selected = ").append(this.selected);
- sb.append(">/n");
- return sb.toString();
- }
- }
给定年月,得到这个月中的天数及每天是星期几(Calendar类的用法)
最新推荐文章于 2021-09-13 19:21:18 发布